Thread: C++ Problems...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    C++ Problems...

    The basic formula of my program works.
    Now I'm just trying to get it to only accept positive input numbers.
    And when it's over, I want it to ask the user if they want to do it again, and if
    they put a Y then do it again, and if they put a N, then quit. This is what I have so far, and I have a few errors. Please help?








    Code:
    #include <iostream>  
    #include <iomanip>
    using namespace std;
    
    double retail (double, double);
    
    int main()
    {
       double cost;
       double markup; 
       char answer = 'y';
    
       while (answer = 'y')||(answer = 'Y')
       {
    
       cout << "What is the item's wholesale cost? ";
       cin >> cost;
       cout << endl;
    
       cout << "What is the markup percentage? ";
       cin >> markup;
       cout << endl; 
    
       if (cost >= 0) && (markup >= 0)
    
       {
    
       cout << "The retail price of the item is $ " << retail (cost, markup) << fixed << setprecision(2) << endl;
    
       }
    
       else 
       {
    		cout << "Please re-enter values using positive numbers only." << endl;
       }
    
       }
    
       cout << "Do you want to try again? (y/n) ";
       cin >> answer;
       cout << endl; 
    
    	return (0);
    
    }
    
    	double retail (double wholesaleCost, double markupPercentage)
       {
    	  
    	   return (wholesaleCost + (wholesaleCost * (markupPercentage/100)));
    
       }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The compiler does tell you what the errors are, you know: you're only allowed one set of parentheses after while. You should also use any reasonable style of indentation, to see where your loops end visually, which will (should) fix all your loop errors.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you always want a loop to be entered, then rather specifically setting things up so that the condition will always be true the first time around, you're supposed to use a do..while loop.

    More parentheses!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Instead of:
    Code:
    while (answer = 'y')||(answer = 'Y')
    Do this:
    Code:
    while ( (answer == 'y') || (answer == 'Y') )
    My Website

    "Circular logic is good because it is."

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    >>Now I'm just trying to get it to only accept positive input numbers.
    So, you want the "int cost" variable, to make sure it is positive, at all times? Right?

    I would have created a new function that looks like this:

    Code:
    int PositivieInteger(char* s, int minimum, int maximum)
    {
        int returnvalue = 0;
        do
        {
        cout << s; //Writes out the text you sent with it
        cin  >> returnvalue;  //Takes input from the user
        }
        while(minimum >= returnvalue || returnvalue >= maximum) ;
        //If input (returnvalue) is less than minimum value or bigger than maximum, force user
        // to input a new number
    
        return returnvalue;
    }
    
    int main()
    {
        int cost = PositiveInteger("Hello, what is the cost of this item?: ", 0, 1000);
        //Now the integer "cost" holds the value from the "returnvalue" (input) from the function above...
    
       return 0;
    }
    You can simply copy paste this code, its working... The function "PositiveInteger", place it above "int main".
    Last edited by ManyTimes; 03-22-2010 at 05:29 PM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by ManyTimes View Post
    Code:
        while(minimum >= returnvalue || returnvalue >= maximum) ;
        //If input (returnvalue) is less than minimum value or bigger than maximum, force user
        // to input a new number
    so min and max values are not permitted by your code?
    your string should be passed as const char* at least
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM