int *getInteger(int **ptr, const char *prompt)
I have to write a function that gets a pointer from a user.
kinda like scanf() but understands spaces(I guess).
So the type of function is:
int *getInteger(int **ptr, const char *prompt);
I know that char is ** ptr going to = &somevar
I'm not sure what int *prompt is going to be. (maybe %d?)
I have to allocate the required memory for the data type .
So the data type will be Integer. And then store this info in ptr with error checking. I'm not sure how to write this, if someone could help.
Ok, I'm just going to copy this from my teachers notes because I don't understand a word this is saying (if someone understands coders trying to explain stuff to people that don't understand).
I would really just like someone to explain to me what the hell he is trying to say, not just give me the anwser.
after the allocation in question 2 I have to:
display the passed in prompt to the user
read in a string (store it in input)
behavior for all functions except getString
determine whether or not the string starts with the appropriate kind of data
if the string contains the wrong kind of data, display an error message and have the user try again (repeat this until the user types in the correct kind of data)
if the string contains the appropriate kind of data, convert from the string to the appropriate data type. Store the new data in the memory allocated earlier.
If someone would like to look at the whole assignment its on:
I'm not really looking for anwsers because I can do this type of stuff with a little bit of help, I just can't catch what he is saying.
"I have to write a function that gets a pointer from a user."
You can't get a pointer from the user. A pointer is a type of variable that stores an address to a location in memory where there is some data. All you can get from the user is data, which you have to store in the appropriate type of variable. In simpler terms, if you needed to get an integer from the user, you might display a message that says, "Please enter an integer: ". You wouldn't ask, "Please enter an integer variable: ", otherwise they might enter: int my_number.
Question 1: unintelligible
Question 2: You have to create a variable that will store the user input. Usually, you are given what data is going to be entered and then you decide what variables are going to handle that. This problem is approaching things in reverse. You need to create the variable type you've been given, and then read data into the variable. If you don't know how to create a variable of one of the basic types and get input from the user, I suggest you buy a good C++ book(Ivor Horton's Beginning C++) or read some online tutorials.
display the passed in prompt to the user --> in the function, you need to write some code that will display the variable called prompt, which is a parameter of the function, to the screen
read in a string (store it in input)-->in the function ask the user for the appropriate input(=string) and store it in the variable called input, which is also a parameter of the function.
behavior for all functions except getString
determine whether or not the string starts with the appropriate kind of data-->make sure characters in the string entered by the user are "appropriate". What is "appropriate"? I can't be sure because the problem doesn't explicitly say so, but given the function names and what you're instructed to do with the input, it looks like you're supposed to check to make sure the user entered ints for the GetInteger() function, doubles for the Get Double() function, etc. You are reading the input into a char variable, which can store anything, so for the GetInteger() function if the user entered: abde, that isn't what you want. You want them to enter something like 9.
if the string contains the wrong kind of data, display an error message and have the user try again (repeat this until the user types in the correct kind of data) --> seems clear
if the string contains the appropriate kind of data, convert from the string to the appropriate data type. Store the new data in the memory allocated earlier-->if the data entered is of the correct form you can use functions like atoi()--alpha to int--to convert the numbers to the appropriate type and store them in the two dimensional array ptr.
Last edited by 7stud; May 8th, 2003 at 05:17 AM.
Re: int *getInteger(int **ptr, const char *prompt)
Part of your confusion seems to be centered around the function and its parameters.
prompt is a character string, or more properly a character array. It is const because the function will use it, but cannot change it. This is the string you will output as a prompt for the user.
ptr is a pointer to an integer array. I only scanned your message quickly, but my understanding is that you will receive a number of integer values from the user which you will then create an array for and store them in that array. ptr has to be a pointer to a pointer to integers so that you can return the address of the memory space you just allocated dynamically (with malloc() if in C, or with new if in C++).
My recommendation is to have a local variable that is an int* and that you use to allocate the array. Then assign that value to *ptr just before you return. Just to be able to keep everything straight (I recently got turned around trying to do the same kind of thing without local variables).
Then it appears that the function's return value is the address of the integer array that you created.
Last edited by dwise1_aol; May 8th, 2003 at 11:21 AM.