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.
// celsius to Fahrenheit Program with a loop//
#include <iostream>
usingnamespace 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;
}
}
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
// celsius to Fahrenheit Program with a loop//
#include <iostream>
usingnamespace 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;".
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.
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:
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.