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.
Code:#include <cctype> #include <iostream> using namespace std; int getInteger(int *num); int main() { int input; int *param; cout << "Enter an integer with a leading + or - sign." << endl; input=getInteger(param); cout << "The integer you entered is " << input; return 0; } int getInteger(int *num) { char c; int sign; int rtrn; int value=0; cin.get(c); do cin.get(c); while(isspace(c)); if (!isdigit(c) && !cin.eof() && c!='+' && c!='-') { rtrn=0; } else { if(c=='-') sign=(-1); else sign=1; if(c=='-' || c=='+') c=cin.get(); while(isdigit(c)) { value=10*value+c-'0'; c=cin.get(); } value=value*sign; if(!cin.eof()) cin.putback(c); rtrn=c; } *num=value; return rtrn; }



LinkBack URL
About LinkBacks


