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 ...

  1. #1
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23

    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.

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Code:
    	cin.get(c);
    	do
    		cin.get(c);
    	while(isspace(c));
    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.

  3. #3
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    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?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> For param, should I change it to &param?
    Yes, if you make param a regular int instead of an int*.

    You should also output param in the "The integer you entered is " part since the assignment says that the param is what will have the result.

  5. #5
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    Woohoo! It works! Thanks so much for the help.

  6. #6
    Kill Clear Channel Kheila's Avatar
    Join Date
    Oct 2005
    Location
    St. Paul, MN
    Posts
    23
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to functions problem
    By sept in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2007, 04:16 PM
  2. Problem with changing variables :pointers, functions
    By SyCo1234 in forum C Programming
    Replies: 16
    Last Post: 04-03-2005, 07:49 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21