Terminating a program with if statement

How do i make it where if g=-99 ONLY -99 then it will terminate the whole program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  /* 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<iostream>
#include<stdlib.h>
#include<stdio.h>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	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.