i am teaching my dad c++ and we made a simple calculator. i was trying to get the program to ask the user if he wanted to restart and i thought i knew how. im not asking for a revised code but any criticism or help is appreciated.
i use xcode if that makes a difference.
#include <iostream>
using namespace std;
double Number1, Number2; // nubmbers to be Mathed
double Answer; // answer has to be a double because the defined numbers are
char String1; // defining my charaters
char Restart;// restart chariter
int main ()
{
cout << " Calculator by Isaac Williams." << endl;
cout << "Enter First Number." << endl;
cin >> Number1;
cout << "Enter Second Number." << endl;
cin >> Number2;
cout << " Please select +,-,*,/ then press enter" << endl;
cin >> String1;
Change your char Restart; to char Restart = 'y'; After cout << Calculator by Isaac Williams." << endl; , add a do {, and then add } while (Restart == 'Y' || Restart == 'y' ); right after your cin >> Restart;
Remove
double Number1, Number2; // nubmbers to be Mathed
double Answer; // answer has to be a double because the defined numbers are
char String1; // defining my charaters
char Keepgoing;// restart chariter
Sorry to say this, but actually you did NOT do what we mentioned. You have no do/while loop. Or the while statement should have been between the first two cout statements, as Disch mentioned, if you didn't want to use a do/while as I mentioned. Put the code in as I described, and you should be pretty close to a running program.
You should also declare your variables inside of main and give them default values.
instead of
1 2 3 4 5 6
double Number1, Number2;
// et cetera
int main( )
{
// et cetera
do this
1 2 3 4
int main( )
{
double Number1, Number2 = 0;
// et cetera...
This is just good practice...
as for your restarting problem, I would recommend using a while(1) or a for(;;) and then breaking out of it (using the break keyword) when you want to quit