Thread: Returning A Value From a Seperate Function

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    23

    Thumbs down Returning A Value From a Seperate Function

    If I had the program:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void getItemPrice()
    {
    	int price;
    	// making sure cost is not negative
    		do
    		{
    			cout << "Enter cost of item (in cents) :" << endl;
    			cin >> price;
    		} while (price <= 0);
    
    		// making sure cost is a multiple of 5
    
    		while ((price&#37;5)!=0)
    		{
    			cout << "Price of item must be multiple of 5 cents." << endl;
    			cout << "Enter cost of item (in cents) :" << endl;
    			cin >> price;
    
    		// again making sure cost is not negative
    		while (price <= 0)
    		{
    			cout << "Enter cost of item (in cents) :" << endl;
    			cin >> price;
    		}
    
    		}
    	return price;
    }
    
    int main();
    {
      int price;
    
      price = getItemPrice();
    
    }
    I compile it and get the a warning saying
    In function 'void getItemPrice()':
    return-statement with a value, in function declared with a void return

    type

    I'm just wondering why I am unable to return the value of price from my getItemPrice function back to the main function. I know the series of commands inside getItemPrice work because I copied and pasted it from the previous assignment that we are building off of.

    Thanks
    Last edited by Adrian; 10-15-2007 at 09:00 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    First of all, getItemPrice will not properly ensure that the price returned is a positive multiple of 5, if the proper sequence of invalid prices is entered by the user. You need to check both these conditions in the same loop.

    Second, a funtions return type is given as the first part of the first line of the function declaration. In this case your functions return type is shown in blue here:
    Code:
    void getItemPrice()
    This return type must match the type of any return statement in the function. If the type is void, the function returns nothing, so you don't have to put a return statement. In this case however, you want to return an int, so you should replace void with int.

    Third, you want to remove the semicolon after int main().
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    I just tried getItemPrice in another program and it did work, I tried every order of negative, non-multiples of 5, and other numbers that I could and it worked every time.

    As for the other things, thanks a bunch, it makes sense now.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Code:
    #include <iostream>
    
    using namespace std;
    
    int getItemPrice()
    {
    	int price;
    	// making sure cost is not negative
    		do
    		{
    			cout << "Enter cost of item (in cents) :" << endl;
    			cin >> price;
    		} while (price <= 0);
    
    		// making sure cost is a multiple of 5
    
    		while ((price&#37;5)!=0)
    		{
    			cout << "Price of item must be multiple of 5 cents." << endl;
    			cout << "Enter cost of item (in cents) :" << endl;
    			cin >> price;
    
    		// again making sure cost is not negative
    		while (price <= 0)
    		{
    			cout << "Enter cost of item (in cents) :" << endl;
    			cin >> price;
    		}
    
    		}
    	return price;
    }
    
    int getCoins(int price)
    {
    	int coin = 0;
    	int balance = 0;
    
    	// start entering coins
    	while (balance < price)
    	{
    	cout << "Current Balance: " << balance << endl;
    	cout << "Enter coin value (200, 100, 25, 10, 5) :" << endl;
    	cin >> coin;
    
    	// for invalid coins
    		if ( coin != 200 && coin != 100 && coin != 25 && coin != 10 && coin != 5)
    		{
    			cout << "Invalid coin." << endl;
    		}
    		else
    		{
    			balance += coin;
    		}
    
    	return balance;
    }
    
    void dispenseChange(int change);
    {
    		// obtain number of quarters given out
    
    	Line 69 ---->	int quarter = change/25;
    		int changeafterq = change % 25;
    
    		// using the remaining money owed try find number of dimes required
    
    		int dime = changeafterq/10;
    		int changeafterd = changeafterq % 10;
    
    		// same thing with nickels
    
    		int nickel = changeafterd/5;
    
    	Line 81 ---->  cout << "You paid " << paid << " cents for something that cost " << getItemPrice() << " cents." << endl;
    		cout << "Giving " << change << " cents in change." << endl;
    
    		// Finally, enter integer values of each coin being given back.
    
    		cout << "Quarters: " << quarter << endl;
    		cout << "Dimes: " << dime << endl;
    		cout << "Nickels: " << nickel << endl;
    }
    
    
    
    int main()
    {
    	int price;
    	int	paid;
    	int	change;
    
    	price = getItemPrice();
    	paid = getCoins(price);
    
    	change = paid - price;
    
    	dispenseChange(change);
    }
    Now I'm getting warnings when I compile that say
    In function 'int getCoins(int)':
    69: 'change' undeclared (first use this function)
    81: 'paid' undeclared (first use this function)

    I marked lines 69 and 81 in the code, but it's not actually marked in my program, this is just so you can see where the errors are occurring.

    I'm slightly confused by the warnings because it says they occur in the function int getCoins, but the lines mentioned correspond with lines that are actually in the function dispenseChange.

    Also, I think I know why 'paid' is undeclared, but I don't know how to declare it since I can't change anything in the main function.

    Thanks.
    Last edited by Adrian; 10-15-2007 at 09:48 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You have the same problem as before. Stop putting semicolons after the first line of a function declaration.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    It says there's a parse error if I don't put the semi-colon there, which I know is strange, but it shuts the compiler up. I took it out and still had the same problems, so that's not what's affecting it. Also, I need the semi-colon after int main() and the extra } at the end of the code or else the compiler will complain about that too.

    Alright, so I've gotten my code to this, everything is perfect (or would be if I could get this problem worked out)

    Code:
    #include <iostream>
    
    using namespace std;
    
    int getItemPrice()
    {
    	int price;
    	// making sure cost is not negative
    		do
    		{
    			cout << "Enter cost of item (in cents) :" << endl;
    			cin >> price;
    		} while (price <= 0);
    
    		// making sure cost is a multiple of 5
    
    		while ((price&#37;5)!=0)
    		{
    			cout << "Price of item must be multiple of 5 cents." << endl;
    			cout << "Enter cost of item (in cents) :" << endl;
    			cin >> price;
    
    		// again making sure cost is not negative
    		while (price <= 0)
    		{
    			cout << "Enter cost of item (in cents) :" << endl;
    			cin >> price;
    		}
    
    		}
    	return price;
    }
    
    int getCoins(int price)
    {
    	int coin = 0;
    	int balance = 0;
    
    	// start entering coins
    	while (balance < price)
    	{
    	cout << "Current Balance: " << balance << endl;
    	cout << "Enter coin value (200, 100, 25, 10, 5) :" << endl;
    	cin >> coin;
    
    	// for invalid coins
    		if ( coin != 200 && coin != 100 && coin != 25 && coin != 10 && coin != 5)
    		{
    			cout << "Invalid coin." << endl;
    		}
    		else
    		{
    			balance += coin;
    		}
    	cout << "You paid " << balance << " cents for something that cost " << price << " cents." << endl;
    
    	return balance;
    }
    
    int dispenseChange(int change);
    {
    		// obtain number of quarters given out
    
    	line 70 ----> int quarter = change/25;
    		int changeafterq = change % 25;
    
    		// using the remaining money owed try find number of dimes required
    
    		int dime = changeafterq/10;
    		int changeafterd = changeafterq % 10;
    
    		// same thing with nickels
    
    		int nickel = changeafterd/5;
    
    
    		cout << "Giving " << change << " cents in change." << endl;
    
    		// Finally, enter integer values of each coin being given back.
    
    		cout << "Quarters: " << quarter << endl;
    		cout << "Dimes: " << dime << endl;
    		cout << "Nickels: " << nickel << endl;
    }
    
    
    int main();
    {
    	int price;
    	int	paid;
    	int	change;
    
    	price = getItemPrice();
    	paid = getCoins(price);
    
    	change = paid - price;
    
    	dispenseChange(change);
    }
    }
    The compiler warns me about:
    70: 'change' undeclared (I marked line 70 in the code, but it's not in my actual program)

    For some reason I guess change isn't being brought up to the function dispenseChange from main.

    If I can get this to work then I think I'll finally be done.

    Thanks again.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Adrian View Post
    It says there's a parse error if I don't put the semi-colon there, which I know is strange, but it shuts the compiler up. I took it out and still had the same problems, so that's not what's affecting it. Also, I need the semi-colon after int main() and the extra } at the end of the code or else the compiler will complain about that too.
    Okay. You've successfully determined that your car runs better after you've bolted a large plastic banana to it. Next up, installation of the gyroscopes.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    Quote Originally Posted by brewbuck View Post
    Okay. You've successfully determined that your car runs better after you've bolted a large plastic banana to it. Next up, installation of the gyroscopes.
    If you're suggesting that it's not necessary to have these in the code, then I get your point, but I'm just going with what the compiler says. I get 0 on the assignment if it doesn't compile properly, so I'm not going to go with what's "theoretically correct" if it's not working in this situation.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You have clearly failed at shutting the compiler up if it is still giving you errors. Just because the error appears several lines down, doesn't mean that that's where the mistake is.

    Those semicolons do not belong there.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Adrian View Post
    If you're suggesting that it's not necessary to have these in the code, then I get your point, but I'm just going with what the compiler says. I get 0 on the assignment if it doesn't compile properly, so I'm not going to go with what's "theoretically correct" if it's not working in this situation.
    Randomly adding semicolons until it compiles is hardly a better choice. Exactly what compiler errors do you get if you don't use them? There is no reason why you need a semicolon after main().

    You've obviously got an extra '{' somewhere in your code, probably far from the site of the actual warning/error.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by brewbuck View Post
    You've obviously got an extra '{' somewhere in your code, probably far from the site of the actual warning/error.
    It's in the getCoins function. The while loop does not have a closing brace.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Code:
    int getCoins(int price)
    {
    	int coin = 0;
    	int balance = 0;
    
    	// start entering coins
    	while (balance < price)
    	{
    	cout << "Current Balance: " << balance << endl;
    	cout << "Enter coin value (200, 100, 25, 10, 5) :" << endl;
    	cin >> coin;
    
    	// for invalid coins
    		if ( coin != 200 && coin != 100 && coin != 25 && coin != 10 && coin != 5)
    		{
    			cout << "Invalid coin." << endl;
    		}
    		else
    		{
    			balance += coin;
    		}
            }
    	cout << "You paid " << balance << " cents for something that cost " << price << " cents." << endl;
    
    	return balance;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Problem in returning value from the dll exported function
    By dattaforit in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2006, 04:30 AM
  4. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM