BTW, here's what I ended up with:
Code:
/***********************************
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;
}