Problem with pointers and functions

This is a discussion on Problem with pointers and functions within the C++ Programming forums, part of the General Programming Boards category; We're covering pointers in my C++ class, and just got done studying functions. I'm trying to write a program that ...

Problem with pointers and functions

We're covering pointers in my C++ class, and just got done studying functions. I'm trying to write a program that uses cin.get() to read input (yes, I know this must be a char value). The cin.get() must be included in the getInteger function, not in main. It must return the value to main, so getInteger can't be a void function. Additionally, num must be a pointer to the integer the user inputs, and must be a parameter of function getInteger. Here's the code I've written. It runs, but terminates after I input an integer. Any ideas would be GREATLY appreciated. I'm completely stuck.

What if the first character isn't a space? this code will ignore the first character no matter what. Remove the first cin.get(c) or change it to a while loop.

Also, you return rtrn, but you set rtrn to c at the end of your else block. Shouldn't rtrn be the value?

Finally, and probably most importantly, you create an int pointer variable called param but don't have it pointing at any memory. You should probably just make it a regular int and pass the address of it to the function.

Okay, I took the first cin.get(c) out, that makes sense. I was wondering why rtrn was set to c at the end, because that part came from my instructor's pseudocode that we're supposed to model the function after. So far, though, he hasn't had a typo in anything he's given us. For param, should I change it to &param?

/***********************************
Lab7a.cpp
by Meghan
10/9/05
Lab 7, part one
A program that uses pointers to get
an integer from user input as a
character value, reads it as an
integer value and passes it back to
the main function, where it is
displayed if it is a valid integer.
************************************/
#include <cctype>
#include <iostream>
using namespace std;
//function prototype
int getInteger(int *num);
int main()
{
int input; /*holds the value of the user's input after it
is converted to an int value in function
getInteger */
int returnedValue; //acts as a test result from function getInteger
cout << "Enter an integer with a leading + or - sign." << endl;
/*calls the getInteger function and passes the address
of input to the pointer parameter in that function*/
returnedValue=getInteger(&input);
/*tests whether the getInteger function found the input
to be valid, and if not, prints an error statement.*/
if (returnedValue==0)
{
cout << "You did not input a valid integer. " << endl;
cout << "It must be a number with a + or - in front of it." << endl;
}
//displays the integer value returned by the getInteger function
else
cout << "The integer you entered is " << input << "." << endl;
return 0;
}
//reads the user's input as a character value and determines whether it is valid
//parameter num points to the address of variable input, located in function main
int getInteger(int *num)
{
char c; //holds character value of user's input
int sign; //holds sign (positive or negative) of user's input
int rtrn; //holds value to be returned to function main
int value=0; //holds integer value of user's input
//gets user's input, skipping leading space
do
cin.get(c);
while(isspace(c));
/*Checks to make sure the inputted character is a digit and the user has actually
entered something. If these are not true, the function will pass 0 back to main and
it will print an error message.*/
if (!isdigit(c) && !cin.eof())
rtrn=0;
/*Checks to make sure the first inputted character is either a '+' or a '-'.
If these are not true, the function will pass 0 back to main and it will
print an error message.*/
if (c!='+' && c!='-')
rtrn=0;
//Runs after the program has made sure the user input is valid.
else
{
//Checks whether the sign of the inputted integer is negative.
if(c=='-')
sign=(-1);
//If the sign is not negative, it must be positive.
else
sign=1;
//Retrieves the next character value from the input stream.
if(c=='-' || c=='+')
c=cin.get();
//As long as the character value in the input stream is a digit...
while(isdigit(c))
{
/*...value stores the value of the digit as an int and places
it in the proper place (i.e. hundreds, tens, ones)...*/
value=10*value+c-'0';
//...and then the program reads the next digit from the input stream.
c=cin.get();
/*Eventually, the absolute value of the entire number from the input
stream is "converted" to an int value and stored in variable value*/
}
/*After all digits have been read, value becomes positive or negative,
depending on the value of sign.*/
value=value*sign;
/*If the input stream contains a non-digit character after reading all
the digits, it puts this character back into the input stream to make
it available for later use.*/
if(!cin.eof())
cin.putback(c);
//sets the value to be returned to c
rtrn=c;
}
/*Stores value of variable value in the memory space pointed to by p, which is
the variable input in function main. */
*num=value;
/*sends the value of rtrn back to function main, which gets stored in
variable returnedValue */
return rtrn;
}