Thread: while loop problem

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    while loop problem

    Hey,

    I find with all my programmes I write, if I have two inputs to enter, within the while loop, it wont end until the last input has been put in, in the past I went round it the long way by putting an 'if' statment in to break the loop after the first input, but what is the correct way to solve this problem, I had a quick look at the QA's and the Search thing, but not much luck
    Code:
    	while (( miles != -1) || ( liters != -1 ))
    	{
    	cout << "Please enter number of miles you have driven or -1 to quit" << endl;
    	cin >> miles;
    	cout << "Please enter number of liters you put in your car to fill it up" << endl;
    	cin >> liters;
    	answer = compute(liters, miles);
    
    	cout << "You have travled " << answer << " miles per gallon" << endl;
    	}
    	return 0;
    Thanks, ps not homework as I am only teaching myself, in the UK I can never find any (cheap) lessions on C++ anyway.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, since you state the part about entering -1 only at the prompt for miles, you'll only need to test miles.
    So the loop condition becomes (miles != -1)

    THe quirk with the way you do it might be that a user could enter -1 for miles, and then still be greeted with a prompt for liters.
    An alternative to fix this might be to use an infinite loop, breaking out of it if miles is tested to be -1.
    Code:
    //loop ends when miles == -1
    for (;;) {
    	//prompt for miles, enter -1 to quit
    	cin >> miles;
    	if (miles == -1)
    		break; //end loop here!
    	//prompt for liters
    	cin >> liters;
    	//compute answer
    	//print result
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    cout << "Please enter number of miles you have driven or -1 to quit" << endl;
    cin >> miles;
    while ( miles != -1 )
    {
        cout << "Please enter number of liters you put in your car to fill it up" << endl;
        cin >> liters;
    
        answer = compute(liters, miles);
        cout << "You have travled " << answer << " miles per gallon" << endl;
    
        cout << "Please enter number of miles you have driven or -1 to quit" << endl;
        cin >> miles;
    }
    return 0;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM