Strings, integers and arrays as pointers [closed]
up vote
-2
down vote
favorite
I'm learning about pointers, and how to use them effectively so I'd like to know if this code is an appropriate use of them.
This code and especially these functions are highly influenced in the Harvard course CS50. This is also a challenge to myself for trying to simulate their functions.
Here are the functions that I've used:
// Function that gets a positive number using a do while validation
int get_positive_int(char* prompt)
{
int i;
do
{
printf("%s", prompt);
scanf("%i", &i);
}
while (i < 1);
return i;
}
// Function that gets a string as a pointer char*
char* get_string(char* prompt, short limit)
{
char *s = new char[limit];
printf("%s", prompt);
cin.getline(s, limit, 'n');
return s;
}
// Function that gets an array of numbers as a pointer of integers
int *get_int_array(char* prompt, short limit)
{
int *iArr = new int[limit];
for (int i = 0; i < limit; i++)
{
printf("number[%i]: ", i);
scanf("%i", iArr + i);
}
return iArr;
}
This is my main program:
int main(void)
{
// Get the name using the string function
char *name = get_string("name: ", 32);
printf("name is %snn", name);
// Get the arrays' size using the int function
int size = get_positive_int("size: ");
printf("array's size is %inn", size);
// Get the array of integers using the int arr function
int *iArr = get_int_array("array: ", size);
// Print them
printf("int array isn");
for (int i = 0; i < size; i++)
cout << "[" << i << "]: " << *(iArr + i) << " in " << &iArr[i] << endl;
printf("n");
// Deleting the allocate memory
delete name;
delete iArr;
}
This is the output:
name: Gilberto
name is Gilberto
size: 4
array´s size is 4
number[0]: 431
number[1]: -32
number[2]: 876
number[3]: -98
int array is
[0]: 431 in 0x563b41e256c0
[1]: -32 in 0x563b41e256c4
[2]: 876 in 0x563b41e256c8
[3]: -98 in 0x563b41e256cc
c++ strings pointers
closed as off-topic by πάντα ῥεῖ, Snowhawk, vnp, Toby Speight, 200_success Dec 3 at 20:03
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Snowhawk, vnp
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-2
down vote
favorite
I'm learning about pointers, and how to use them effectively so I'd like to know if this code is an appropriate use of them.
This code and especially these functions are highly influenced in the Harvard course CS50. This is also a challenge to myself for trying to simulate their functions.
Here are the functions that I've used:
// Function that gets a positive number using a do while validation
int get_positive_int(char* prompt)
{
int i;
do
{
printf("%s", prompt);
scanf("%i", &i);
}
while (i < 1);
return i;
}
// Function that gets a string as a pointer char*
char* get_string(char* prompt, short limit)
{
char *s = new char[limit];
printf("%s", prompt);
cin.getline(s, limit, 'n');
return s;
}
// Function that gets an array of numbers as a pointer of integers
int *get_int_array(char* prompt, short limit)
{
int *iArr = new int[limit];
for (int i = 0; i < limit; i++)
{
printf("number[%i]: ", i);
scanf("%i", iArr + i);
}
return iArr;
}
This is my main program:
int main(void)
{
// Get the name using the string function
char *name = get_string("name: ", 32);
printf("name is %snn", name);
// Get the arrays' size using the int function
int size = get_positive_int("size: ");
printf("array's size is %inn", size);
// Get the array of integers using the int arr function
int *iArr = get_int_array("array: ", size);
// Print them
printf("int array isn");
for (int i = 0; i < size; i++)
cout << "[" << i << "]: " << *(iArr + i) << " in " << &iArr[i] << endl;
printf("n");
// Deleting the allocate memory
delete name;
delete iArr;
}
This is the output:
name: Gilberto
name is Gilberto
size: 4
array´s size is 4
number[0]: 431
number[1]: -32
number[2]: 876
number[3]: -98
int array is
[0]: 431 in 0x563b41e256c0
[1]: -32 in 0x563b41e256c4
[2]: 876 in 0x563b41e256c8
[3]: -98 in 0x563b41e256cc
c++ strings pointers
closed as off-topic by πάντα ῥεῖ, Snowhawk, vnp, Toby Speight, 200_success Dec 3 at 20:03
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Snowhawk, vnp
If this question can be reworded to fit the rules in the help center, please edit the question.
Why is this question tagged as c++, when it doesn't look like you are using any C++ features or idioms at all?
– 200_success
Dec 1 at 1:49
2
@200_success the use ofcoutandcin.getline()actually indicate this is C++ and not C. Or the bastard child of both.
– bruglesco
Dec 1 at 2:23
2
Can you give a summary of the intended purpose of this code, and more specifics on what kind of review you would like? Are you trying to limit yourself to not using parts of the standard library, and if so, which parts? There are plenty of easier ways to read input than what you have here, so we need to know exactly what limitations you've placed on yourself to give a more helpful review.
– cariehl
Dec 3 at 17:47
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I'm learning about pointers, and how to use them effectively so I'd like to know if this code is an appropriate use of them.
This code and especially these functions are highly influenced in the Harvard course CS50. This is also a challenge to myself for trying to simulate their functions.
Here are the functions that I've used:
// Function that gets a positive number using a do while validation
int get_positive_int(char* prompt)
{
int i;
do
{
printf("%s", prompt);
scanf("%i", &i);
}
while (i < 1);
return i;
}
// Function that gets a string as a pointer char*
char* get_string(char* prompt, short limit)
{
char *s = new char[limit];
printf("%s", prompt);
cin.getline(s, limit, 'n');
return s;
}
// Function that gets an array of numbers as a pointer of integers
int *get_int_array(char* prompt, short limit)
{
int *iArr = new int[limit];
for (int i = 0; i < limit; i++)
{
printf("number[%i]: ", i);
scanf("%i", iArr + i);
}
return iArr;
}
This is my main program:
int main(void)
{
// Get the name using the string function
char *name = get_string("name: ", 32);
printf("name is %snn", name);
// Get the arrays' size using the int function
int size = get_positive_int("size: ");
printf("array's size is %inn", size);
// Get the array of integers using the int arr function
int *iArr = get_int_array("array: ", size);
// Print them
printf("int array isn");
for (int i = 0; i < size; i++)
cout << "[" << i << "]: " << *(iArr + i) << " in " << &iArr[i] << endl;
printf("n");
// Deleting the allocate memory
delete name;
delete iArr;
}
This is the output:
name: Gilberto
name is Gilberto
size: 4
array´s size is 4
number[0]: 431
number[1]: -32
number[2]: 876
number[3]: -98
int array is
[0]: 431 in 0x563b41e256c0
[1]: -32 in 0x563b41e256c4
[2]: 876 in 0x563b41e256c8
[3]: -98 in 0x563b41e256cc
c++ strings pointers
I'm learning about pointers, and how to use them effectively so I'd like to know if this code is an appropriate use of them.
This code and especially these functions are highly influenced in the Harvard course CS50. This is also a challenge to myself for trying to simulate their functions.
Here are the functions that I've used:
// Function that gets a positive number using a do while validation
int get_positive_int(char* prompt)
{
int i;
do
{
printf("%s", prompt);
scanf("%i", &i);
}
while (i < 1);
return i;
}
// Function that gets a string as a pointer char*
char* get_string(char* prompt, short limit)
{
char *s = new char[limit];
printf("%s", prompt);
cin.getline(s, limit, 'n');
return s;
}
// Function that gets an array of numbers as a pointer of integers
int *get_int_array(char* prompt, short limit)
{
int *iArr = new int[limit];
for (int i = 0; i < limit; i++)
{
printf("number[%i]: ", i);
scanf("%i", iArr + i);
}
return iArr;
}
This is my main program:
int main(void)
{
// Get the name using the string function
char *name = get_string("name: ", 32);
printf("name is %snn", name);
// Get the arrays' size using the int function
int size = get_positive_int("size: ");
printf("array's size is %inn", size);
// Get the array of integers using the int arr function
int *iArr = get_int_array("array: ", size);
// Print them
printf("int array isn");
for (int i = 0; i < size; i++)
cout << "[" << i << "]: " << *(iArr + i) << " in " << &iArr[i] << endl;
printf("n");
// Deleting the allocate memory
delete name;
delete iArr;
}
This is the output:
name: Gilberto
name is Gilberto
size: 4
array´s size is 4
number[0]: 431
number[1]: -32
number[2]: 876
number[3]: -98
int array is
[0]: 431 in 0x563b41e256c0
[1]: -32 in 0x563b41e256c4
[2]: 876 in 0x563b41e256c8
[3]: -98 in 0x563b41e256cc
c++ strings pointers
c++ strings pointers
edited Dec 3 at 17:34
Mast
7,43963686
7,43963686
asked Dec 1 at 1:16
Gilberto Marcano
41
41
closed as off-topic by πάντα ῥεῖ, Snowhawk, vnp, Toby Speight, 200_success Dec 3 at 20:03
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Snowhawk, vnp
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by πάντα ῥεῖ, Snowhawk, vnp, Toby Speight, 200_success Dec 3 at 20:03
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Snowhawk, vnp
If this question can be reworded to fit the rules in the help center, please edit the question.
Why is this question tagged as c++, when it doesn't look like you are using any C++ features or idioms at all?
– 200_success
Dec 1 at 1:49
2
@200_success the use ofcoutandcin.getline()actually indicate this is C++ and not C. Or the bastard child of both.
– bruglesco
Dec 1 at 2:23
2
Can you give a summary of the intended purpose of this code, and more specifics on what kind of review you would like? Are you trying to limit yourself to not using parts of the standard library, and if so, which parts? There are plenty of easier ways to read input than what you have here, so we need to know exactly what limitations you've placed on yourself to give a more helpful review.
– cariehl
Dec 3 at 17:47
add a comment |
Why is this question tagged as c++, when it doesn't look like you are using any C++ features or idioms at all?
– 200_success
Dec 1 at 1:49
2
@200_success the use ofcoutandcin.getline()actually indicate this is C++ and not C. Or the bastard child of both.
– bruglesco
Dec 1 at 2:23
2
Can you give a summary of the intended purpose of this code, and more specifics on what kind of review you would like? Are you trying to limit yourself to not using parts of the standard library, and if so, which parts? There are plenty of easier ways to read input than what you have here, so we need to know exactly what limitations you've placed on yourself to give a more helpful review.
– cariehl
Dec 3 at 17:47
Why is this question tagged as c++, when it doesn't look like you are using any C++ features or idioms at all?
– 200_success
Dec 1 at 1:49
Why is this question tagged as c++, when it doesn't look like you are using any C++ features or idioms at all?
– 200_success
Dec 1 at 1:49
2
2
@200_success the use of
cout and cin.getline() actually indicate this is C++ and not C. Or the bastard child of both.– bruglesco
Dec 1 at 2:23
@200_success the use of
cout and cin.getline() actually indicate this is C++ and not C. Or the bastard child of both.– bruglesco
Dec 1 at 2:23
2
2
Can you give a summary of the intended purpose of this code, and more specifics on what kind of review you would like? Are you trying to limit yourself to not using parts of the standard library, and if so, which parts? There are plenty of easier ways to read input than what you have here, so we need to know exactly what limitations you've placed on yourself to give a more helpful review.
– cariehl
Dec 3 at 17:47
Can you give a summary of the intended purpose of this code, and more specifics on what kind of review you would like? Are you trying to limit yourself to not using parts of the standard library, and if so, which parts? There are plenty of easier ways to read input than what you have here, so we need to know exactly what limitations you've placed on yourself to give a more helpful review.
– cariehl
Dec 3 at 17:47
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why is this question tagged as c++, when it doesn't look like you are using any C++ features or idioms at all?
– 200_success
Dec 1 at 1:49
2
@200_success the use of
coutandcin.getline()actually indicate this is C++ and not C. Or the bastard child of both.– bruglesco
Dec 1 at 2:23
2
Can you give a summary of the intended purpose of this code, and more specifics on what kind of review you would like? Are you trying to limit yourself to not using parts of the standard library, and if so, which parts? There are plenty of easier ways to read input than what you have here, so we need to know exactly what limitations you've placed on yourself to give a more helpful review.
– cariehl
Dec 3 at 17:47