I'm a high school student going into programming classes next year so I wanted to get a hang for C++. I had some prior knowledge of the program but this is my first project. (Not commented I apologize)
#include <iostream>
using namespace std;
int main()
{ int choice, redo;
{if (redo==3);
cout<<"Press 1 if you would like to convert Celsius to Farenheit."<<endl;
cout<<"Press 2 if you would like to convert Farenheit to Celsius: ";
double ctemp, ftemp;
cin>>choice;
if (choice==1)
{cout<<"Enter Celsius temperature: ";
cin>>ctemp;
ftemp=(ctemp*5/9)+32;
cout<<"The Farenheit temperature is: "<<ftemp<<endl;
}
else if (choice==2)
{cout<<"Enter Farenheit temperature: ";
cin>>ftemp;
ctemp=((ftemp-32)*5/9);
cout<<"The Celsius temperature is: "<<ctemp<<endl;
cin.get();
}
cout<<"Would you like to do another conversion? If you do, enter 3: ";
cin>>redo;
}
}
I know this will probably look absolutely horrendous to the trained eye but the program will not loop back to the beginning Press 1/Press 2. I would like assistance on being able to loop the program back to the beginning so another conversion can be done. (Not much experience with the "while" loop function)
int choice, redo;
double ctemp, ftemp;
while (redo=3)
{
cout<<"Press 1 if you would like to convert Celsius to Farenheit."<<endl;
cout<<"Press 2 if you would like to convert Farenheit to Celsius: ";
cin>>choice;
if (choice==1)
{
cout<<"Enter Celsius temperature: ";
cin>>ctemp;
ftemp=(ctemp*5/9)+32;
cout<<"The Farenheit temperature is: "<<ftemp<<endl;
}
elseif (choice==2)
{
cout<<"Enter Farenheit temperature: ";
cin>>ftemp;
ctemp=((ftemp-32)*5/9);
cout << "The Celsius temperature is: " << ctemp << endl;
cin.get();
}
cout<<"Would you like to do another conversion? If you do, enter 3: ";
cin>>redo;
}
}
I think the mistake you made in the original was putting a ';' after if(redo==3)
I changed that so it uses a while statement, which makes it so that code always runs while redo = 3.
Another useful tip, declare all your variables up top; it makes it a lot easier to keep track of everything once your programs get more advanced and long.
Thank you very much for the quick and useful response! :D
It works great now! Just wondering, why does it have to be =3 and not ==3? I apologize for my noobiness.