### Terminating a program with if statement

How do i make it where if g=-99 ONLY -99 then it will terminate the whole program?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` `````` /* File name: xxxxxxx * Author: xxxxx * Date: 9/23/2013 * Description: This program will calculate how many watts are generated per day by a single wind turbine and convert it into it's appropriate conversion. */ #include #include #include using namespace std; int main() { double g; //generator's nameplates double s; //average wind speed double w; //watts int i=0; // repeat //Intro message cout << "Greetings, we will be calculation watts per mile per hour\n"; //Collecting info while (i==0) { cout << "Insert Generator's name capacity in watts (type -99 to exit)\n"; cin >> g; if (g < 300 && g != -99) { cout << "Nameplate capacity must be between 300 and 8,000,000 watts.\n"; } else if (g > 8000000) { cout << "Nameplate capacity must be less than 8,000,000.\n"; } else if (g >= 300 && g <= 8000000) { break; } if(g=-99) { cout << "Thanks for using this program!\n"; exit(0); } } while (i==0) { cout << "Insert today's average daily wind speed\n"; cin >> s; if (s < 0) { cout << "Wind speed must be greater than zero.\n"; } else if ((s >= 0) && (s < 6)) { cout << "Wind speed is not sufficient to power the generator.\n"; exit(0); } else if ((s >= 39) && (s <= 73)) { cout << "Tropical storm wind speeds. Wind turbine not operating.\n"; exit(0); } else if (s > 73) { cout << "Time to buy a new wind turbine.\n"; exit(0); } else if (s >= 6 && s <= 38) { break; } } w = g*(s/28)*(s/28)*(s/28); if(w < 1000) { printf("Your windmill generated %.1f watts today!\n", w); } else if((w > 1000) && (w < 1000000)) { w = w/1000; printf("Your windmill generated %.1f kilowatts today!\n", w); } else if(w > 1000000) { w = w/1000000; printf("Your windmill generated %.1f megawatts today!\n", w); } } ``````
Anybody? I'm almost done with this program and this is the only thing I have left for it to be done.
do you mean you want the entire program to loop until you enter -99?
No, I want it to only loop with (g<300) and (g>8 million) excluding -99 which I want to use to terminate the whole program.
Like.

 ```welcome, enter a number for nameplate 6 must be between 300-8 mil. Enter a number for nameplate. 8.1mil its over 8 mil Enter a number for nameplate. -99 Thanks for using this program.```

END
Last edited on
I changed your while loop to this
 ``1234567891011121314151617181920212223`` `````` while (i==0) { cout << "Insert Generator's name capacity in watts (type -99 to exit)\n"; cin >> g; if (g < 300 && g != -99) { cout << "Nameplate capacity must be between 300 and 8,000,000 watts.\n"; } if (g > 8000000) { cout << "Nameplate capacity must be less than 8,000,000.\n"; } if (g >= 300 && g <= 8000000) { break; } if(g==-99) { cout << "Thanks for using this program!\n"; exit(0); } }``````
Last edited on
You have a while loop for a reason why not use it?

`while( g > 299 && g < 8e6 && g != -99 )`

or even while( g != -99 )[/code] but i would suggest method one.

Your i == 0 loops are basically the same as while( true ) , while( 1 ) , while( anything ) because all you did was declare int i = 0 then it appears you are doing nothing with it.
He would still need to take out the else statements
No he should be using if / else if and not a bunch of if's like you have. It is best practice to use if/else if vs if if if if if
Topic archived. No new replies allowed.