while loop problem

This is a discussion on while loop problem within the C++ Programming forums, part of the General Programming Boards category; Hey, I find with all my programmes I write, if I have two inputs to enter, within the while loop, ...

  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
    21,775
    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
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,801
    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

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