Having some trouble

I am pretty new to C++, and I am having some trouble with a simple program. I've been working on it for a while but cant get the loop to work right. Basically no matter what I type in for userinput the loop will continue instead of break. I've tried switching it around but then the loop will only break and not continue. Any help would be appreciated. Thanks.

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
  // celsius to Fahrenheit Program with a loop//

#include <iostream>
using namespace std;

float usercelsius;
float fahrenheit;
int userinput;

int main()
{
	do
	{

		cout << "Degrees in Celsius: ";
		cin >> usercelsius;
		fahrenheit = usercelsius * 1.8 + 32;
		cout << "Degrees in Fahremheit: " << fahrenheit << "\n";

		cout << "Do you want to do another calculation?\n" << "Enter 1 for yes, Enter 2 for no: ";
		cin >> userinput;

		if (int userinput = 1);

		continue;

		while (int userinput = 2);

		break;
	}

	while (userinput = 2);

	{

		if (int userinput = 2);

		cout << "Thanks for using the program!\n";


		return 0;
	}

}
Last edited on
The semi-colon (;) is used to separate statements. You can't put a semi-colon at the end of your "if" or "while" statements like you've done. That's the reason it never goes to continue or break.

Also, you've declared userinput once already. No need to declare it any more...

Also, be careful where you put the curly brackets ({}), because what you've done here is crazy talk. :P

Here's your code below:

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
  // celsius to Fahrenheit Program with a loop//

#include <iostream>
using namespace std;

float usercelsius;
float fahrenheit;
int userinput;

int main()
{
	while (true)
	{
		cout << "Degrees in Celsius: ";
		cin >> usercelsius;
		fahrenheit = usercelsius * 1.8 + 32;
		cout << "Degrees in Fahremheit: " << fahrenheit << "\n";

		cout << "Do you want to do another calculation?\n" << "Enter 1 for yes, Enter 2 for no: ";
		cin >> userinput;

		if (userinput == 1)
		{
			continue;	
		}
		else
		{
			break;
		}
	}

    if (userinput = 2)
    {
	cout << "Thanks for using the program!\n";
    }
    return 0;
}


EDIT: while (true) is used to create an infinite loop. The loop is terminated when you enter "2" and you go to "break;".
Last edited on
Line 32: you assign to the variable instead of comparing to it

Line 23/36: you create a new variable instead of comparing to an existing one

http://www.cplusplus.com/faq/beginners/logic/

@sasauke: with a do-while loop you must put a semicolon after the while statement.
Last edited on
Wow, thank you for the quick answers. Although I am still having some trouble. I changed it up so I declared userinput. I've only been doing this for about a day lol, so LB I dont quite get what your saying. sasuke, I changed up the code how you outlined and was still stuck in the loop so I changed it up after reading the link from LB. I should probably get myself a book haha. Plus I keep thinking in terms of Python.

This is what I got now.

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
// celsius to Fahrenheit Program with a loop//

#include <iostream>
using namespace std;

float usercelsius;
float fahrenheit;
bool userinput = 1;

int main()
{
	while (bool userinput = 1)
	{

		cout << "Degrees in Celsius: ";
		cin >> usercelsius;
		fahrenheit = usercelsius * 1.8 + 32;
		cout << "Degrees in Fahremheit: " << fahrenheit << "\n";

		cout << "Do you want to do another calculation?\n" << "Enter 1 for yes, Enter 0 for no: ";
		cin >> userinput;

		if (bool userinput = 1)
		{
			continue;
		}
		
		else
		{
			break;
		}

	}

	if (bool userinput = 0)
	{
		cout << "Thanks for using the program!\n";
	}

	return 0;
	

}


still doing the same thing. the loop wont break. and I cant put

if (bool userinput == 1)

the builder keeps saying it's expecting = and not ==

thanks again for any help you can give me. I think I am skipping ahead too quickly in learning.
Last edited on
NOTE: If you enter a value other than 0 or 1 when asked if another calculation is required the program goes into a continual loop.

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
// celsius to Fahrenheit Program with a loop//

#include <iostream>
using namespace std;

float usercelsius;
float fahrenheit;
bool userinput = true;

int main()
{
	while (userinput == true)
	{
		cout << "Degrees in Celsius: ";
		cin >> usercelsius;
		fahrenheit = usercelsius * 1.8 + 32;
		cout << "Degrees in Fahremheit: " << fahrenheit << "\n";

		cout << "Do you want to do another calculation?\n" << "Enter 1 for yes, Enter 0 for no: ";
		cin  >> userinput;


		if (userinput == true)
		{
			continue;
		}

		else
		{
			break;
		}

	}

	if (userinput == false)
	{
		cout << "Thanks for using the program!\n";
	}

	return 0;
}

Last edited on
You don't want the bool on lines 12/23/35. You only need to put that at the line where you are first declaring the variable (line 8 in your code).

1
2
bool x; //I have a boolean called 'x'
x = true; //now x is true; I don't need to specify it's a boolean again, the compiler already knows 
Don't declare a variable inside the while's parenthesis: What you're doing is recreating it and resetting it every time you're checking its value, so it never has a chance to change! This is what you did:

while(bool thing = 1){}

What you have to do, as others said above, is to declare it and assign it a value outside the loop. Future uses of the variable don't need to specify it as a bool. Also, use the "==" comparator symbol, as it is different from the "=" assignment symbol. Do this:

1
2
3
4
5
6
7
8
9
10
11
bool thing = 1;

int main()
{

    while(thing == 1)
    {

    }

}
Last edited on
Thank you guys for the help. I got it to work! lol.

An yeah CodeWriter, I know it will go off if I put in any thing else as userinput. I Don;t want to get too complicated just yet. I am very new to C++ and it took me long enough to get the loop to work right.

Thanks for all the advice and pointers as well guys. I'm think I'm going to back track a little bit and find a better tutorial to continue with.
Topic archived. No new replies allowed.