Public Group

# C++ Loops problems

This topic is 4231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
since this is homework i'll be vague: the syntax for a do loop is:

do {    //whatever logic you want} while ( condition );

you do not have the "while( condition );" part of the loop so it's not compiling.

-me

##### Share on other sites
so let me get this straight. I put "while" after the cout's and stuff, and then i just put parentheses around the if statements?

##### Share on other sites
do {int input;cout << "1. Gas Mileage" << endl << "2. Approximate EXP" << endl << "3. Quit" << endl;cout << endl << "Enter 1, 2, or 3" << endl;cin >> input;if (input == 1){gas();}else if (input = 2){}else if (input = 3){return 0;}else {cout << "Re-enter a number" << endl;}} while (condition);    // <---<--- while goes here

Edit: sheesh!

[Edited by - DigiDude on February 13, 2007 9:23:59 PM]

##### Share on other sites
I didn't bother scanning the entire post for errors after seeing this:

...
if (input == 1){
gas();
}
else if (input = 2){

}
else if (input = 3){
...

Your first comparison is ok, but look at the other two.

##### Share on other sites
Quote:
 Original post by slowmikeI didn't bother scanning the entire post for errors after seeing this:...if (input == 1){gas();}else if (input = 2){}else if (input = 3){...Your first comparison is ok, but look at the other two.

Yeah that only works with VB.LOL

##### Share on other sites
oh durr i didnt have the condition, i get it but do i do it like-------------------

while (input (!=1) || (!=2) || (!=3)){
cout << "Re-enter a number" << endl;

}

that doesn't seem to work, but i know what i'm trying to say, do it while the input doesn't equal 1, 2 or 3.

Yeah i'm starting to pick up more. After a semester of VB, it seems to be kinda hard to switch to C++.

##### Share on other sites
Quote:
 Original post by rozach27oh durr i didnt have the condition, i get it but do i do it like-------------------while (input (!=1) || (!=2) || (!=3)){cout << "Re-enter a number" << endl; }that doesn't seem to work, but i know what i'm trying to say, do it while the input doesn't equal 1, 2 or 3. Yeah i'm starting to pick up more. After a semester of VB, it seems to be kinda hard to switch to C++.

Yeah, the logic of that while loop quoted above would return true everytime, which is not what you're trying to do. Your original setup looked fine.

Quote:
 ...if (input == 1){gas();}else if (input = 2){}else if (input = 3){...Your first comparison is ok, but look at the other two.

What I meant by this was to look at your (=) equal signs. This first one is correct, but the other two are missing the extra sign.

##### Share on other sites
Quote:
 Original post by rozach27oh durr i didnt have the condition, i get it but do i do it like-------------------while (input (!=1) || (!=2) || (!=3)){cout << "Re-enter a number" << endl; }that doesn't seem to work, but i know what i'm trying to say, do it while the input doesn't equal 1, 2 or 3.

Take a look at De Morgan's Law, and think about what you are trying to do:
Ø(input == 1 \/ input == 2 \/ input == 3)

Also remember that, however natural it may feel to write
while (input != 1 || 2 || 3 || ...)
you need to write the full comparison every time, because in between each logic operator is a complete expression (ie, the != does not carry throughout the ORs/ANDs/Whatever):
while (input != 1 || input != 2 || input != 3 || ...)

[Edited by - Driv3MeFar on February 14, 2007 12:14:17 AM]

##### Share on other sites
In addition to all that's been said, you should check out your gas( ) function:
void gas(){int start, end, total;double mpg;cout << "Starting mileage: " << endl;cin >> start;double sum = 0.0, value = 0;int count = 0;while (value != 999){cin >> value;}sum = sum + value;count++;cout << "Ending mileage: ";cin >> end;total = end - start;mpg = total / value;cout << "average: " << sum/count << endl;system( "Pause");}

In particular, look at this:
while (value != 999){cin >> value;}
It seems to me that you'd want to keep a running tally of the sum as well as the count. All that gets executed each time through that while loop, however, is cin >> value. That'll change the value of value (heh. perhaps change that variable name?), but not much else. In other words, think about putting "sum = sum + value" (sum += value ?) and "count++" into the while loop.

-jouley

1. 1
2. 2
3. 3
Rutin
21
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007205

×