Loop problem

I'm having trouble with this while loop, I want it to keep reprompting the user if they put a number less than 300 and more than 8,000,000. However it keeps giving me "Nameplate capacity must be between 300 and 8,000,000 watts." and keeps saying it for an indefinite time only for me to press ctrl + c to get out of it. (Bare in mind that this is not the whole code but it only excutes this much anyways at the moment.)
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
#include<iostream>
#include<stdlib.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
        cout << "Insert Generator's name capacity in watts\n";
        cin >> g;
        while (i==0)
        {
        if (g < 300)
        {
                cout << "Nameplate capacity must be between 300 and 8,000,000 watts.\n";
        }
        else if (g > 8,000,000)
        {
                cout << "Nameplate capacity must be less than 8,000,000.\n";
        }

        if ((g >= 300)&&(g <= 8,000,000))
         {
                break;
        }
        exit (-99);
        }
Last edited on
1
2
cout << "Insert Generator's name capacity in watts\n";
cin >> g;


I guess you want this inside your loop, not outside.
you already posted this qustion before
I got no result from it and i kinda phrased the other question badly.
because you didn't perform any operation to make the condition on your while to be false thus causing an infinite loop

I've revised your code in your post before:
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
#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
	double g; //generator's nameplates
	double s; //average wind speed
	double w; //watts
	
	//Intro message
	cout << "Greetings, we will be calculation watts per mile per hour\n";
	
	//Collecting info
	cout << "Insert Generator's name capacity in watts\n";
	
    /**** Select your preferred code:
    cin >> g;
	while ( g < 300 || g > 8000000 ) {
        cout << "Nameplate capacity must be between 300 and 8,000,000 watts\n";
        cin >> g;
    }
	/////////// OR ////////////////
    while ( true ) {
        cin >> g;
        if ( g < 300 || g > 8000000 ) {
            cout << "Nameplate cap. must be beetween ... \n";
        else if ( g > 8,000,000 ) { // this doesn't make sense this will just run again ?
            cout << "Nameplate cap must be less than 8000000\n";
        else
            break;
    }
    ***/
    exit(-99); // I don't know what is the purpose of this

	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";
	}
	else if ((s >= 39) && (s <= 73))
	{
		cout << "Tropical storm wind speeds. Wind turbine not operating.\n";
	}
	else if (s > 73)
	{
		cout << "TIme to buy a new wind turbine.\n";
	}
    
    return 0;
    
}
Topic archived. No new replies allowed.