C++ Loops problems

Started by
21 comments, last by nemesisgeek 17 years, 1 month ago
I just started C++ in January and i'm a bit confused. I am susposed to make this program ---- This program will perform two separate tasks. Each task will have its own function. When the program is run, the user will be able to choose the task they wish to perform from a menu. Menu: 1. Gas Mileage 2. exp(x) 3. Quit The program should run continuously until the user enters a 3. Task 1: Gas Mileage – Write a C++ function that will calculate average gas mileage for a car. The function should prompt the user to input a starting mileage. Then, the user will be prompted to enter how many gallons were entered at each fill up. The user will continue to enter fill-up amounts until they enter a sentinel value of 999 at which point they should be prompted for the ending mileage. Calculate the miles per gallon based upon the amount of fuel used and the number of miles traveled and display the result neatly formatted to 2 decimal places. Task 2: Approximating – There exists a function called exp(x) that approximates the value of e to the power x. The function is as follows: Write a function that will prompt the user to enter a value for x and then use the exp(x) function to approximate . Carry the series to exactly 10 terms for your calculation, and round to 5 decimal places. Program Usability To make the program as user-friendly as possible, please use if-then statements to prevent the user from entering anything but 1, 2, or 3 at the menu. BUT!!! I'm having problems with the first one, the gas program. Here is my code so far. I have one error, something about the system identifier. #include <cmath> #include <iostream> using namespace std; void gas(); int main (){ 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; } } system( "Pause"); } 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"); } ANy help would be appricated.
Advertisement
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
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?
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]
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
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.
Quote:Original post by slowmike
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.


Yeah that only works with VB.LOL
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
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++.
Quote:Original post by rozach27
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++.


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.
Quote:Original post by rozach27
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.


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]
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

This topic is closed to new replies.

Advertisement