Stop the infinite loop without terminating the program

So far i'm trying to do a while loop but all I get is an infinite loop.
It keeps displaying a message forever until you press ctrl + c. how to stop it and proceed without terminating the 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
/* File name:
 * Author:
 * Date:
 * Description:
*/

#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";
	cin >> g;
	
	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";
        }
	exit(-99);

	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";
	}
}
You can use break like this:
1
2
3
4
5
6
7
8
while(true) // or you can use for(;;)
{
    //Your code...
    //More of your code
    //And more...
    if(condition)  break; //This will stop the infinite loop if the condition is true
    //More code if the condition is false
}
Last edited on
There's also a fundamental flaw in your code.
On line 28, your condition is troublesome.


When writing integer values such as eight million, you omit the commas.
Effectively, what you've written is a sequence of expressions with the use of the binary comma operator.

Here's some pseudo-code, where the separate expressions are separated with parentheses and commas.

else if ((g>8), (000), (000)) {

The first two expressions (g>8) and (000) will be evaluated and discarded. Then, the right-most expression (000) will be evaluated and returned.
Because that expression is not a non-zero value, it will return false, and the block of code will never execute.
another:

1
2
3
4
5
6
7
8
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";
        }

Instead of writing this code, you can simplify it using logical operators:
1
2
3
4
if ( g < 300 || g > 8000000 ) {
    cout << "Nameplate capacity must be between bla bla";
    exit (-99); // you'll want to put exit here instead otherwise ... your program will just exit
}
Last edited on
I know already, if I could I would but my instructor wants me to do otherwise.
Also, what I meant that if i insert 6 for example when its prompting me the nameplate thing, it will keep saying "Nameplate capacity must be between 300 and 8,000,000 watts." Like.
Nameplate capacity must be between 300 and 8,000,000 watts.Nameplate capacity must be between 300 and 8,000,000 watts.Nameplate capacity must be between 300 and 8,000,000 watts.Nameplate capacity must be between 300 and 8,000,000 watts.Nameplate capacity must be between 300 and 8,000,000 watts.Nameplate capacity must be between 300 and 8,000,000 watts.Nameplate capacity must be between 300 and 8,000,000 watts.Nameplate capacity must be between 300 and 8,000,000 watts.Nameplate capacity must be between 300 and 8,000,000 watts.Nameplate capacity must be between 300 and 8,000,000 watts.
Last edited on
aha.. so that is where you want to add loop ?

1
2
3
4
5
6

cin >> g;
while ( g < 300 || g > 8000000 ) {
    cout << "Nameplate must be bla bla";
    cin >> g;
}


more info: http://www.cplusplus.com/doc/tutorial/control/
Last edited on
AH! Thanks! It works now.
Topic archived. No new replies allowed.