need help

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;

switch (String1) {
case '+' : // addition
Answer = Number1 + Number2;
break;

case '-' : // subtraction
Answer = Number1 - Number2;
break;

case '*' :
Answer = Number1 * Number2;
break;

case '/':
Answer = Number1 / Number2;
break;
default:
cout << "error Please Try Agian,\n Use Symbols Given" << endl;
break;
}
cout << "The Answer is: " << Answer << endl;

cout << "run program agian \n y \n n" << endl;
cin >> Restart;

if ( Restart == 'y' || Restart == 'Y' )
{
cout << "Restarting... \n \n \n" << endl;
main ();
} else return 0;






}

Do not call main. Ever.

If you want code to repeat itself, put it in a loop:

1
2
3
4
while( /*condition for looping*/ )
{
  // this code will keep running as long as above condition is true
}


or a do-while loop which is similar:

1
2
3
4
do
{
  // code to loop
}while( /* condition */ );


The difference being the do-while loop runs at least once without checking the condition, and only runs more times if the condition is true.

So in your case you would want something like this:

1
2
3
4
5
6
char keepgoing = 0;
do
{
  // does user want to keep going?
  cin >> keepgoing;
}while( keepgoing == 'y' );
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
1
2
3
4
5
if ( Restart == 'y' || Restart == 'Y' )
{
cout << "Restarting... \n \n \n" << endl;
main ();
} else 
. And you really should NEVER call main() in your program.
i did as you said and thank you but now it says "stopped at break point" this is the cout << "calculator by Isaac Williams" << endl;
Re-post what you have as the source code, so we may see what the new problem, might be. Thanks.
#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 Keepgoing;// 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;

switch (String1) {
case '+' : // addition
Answer = Number1 + Number2;
break;

case '-' : // subtraction
Answer = Number1 - Number2;
break;

case '*' :
Answer = Number1 * Number2;
break;

case '/':
Answer = Number1 / Number2;
break;
default:
cout << "error Please Try Agian,\n Use Symbols Given" << endl;
break;
}
cout << "The Answer is: " << Answer << endl;

{
cout << "run program agian \n y \n n" << endl;
cin >> Keepgoing ;


} while (Keepgoing == 'y');

}
you don't have anything in the while loop to make it start at the top again.

and you don't have the input in a loop either, if you enter a wrong operator it will just cout an undeclared variable, not good.

why is it a bad idea to make main recursive? how is it different from any other recursive function.
@bluecollarmillonare

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.
Last edited on
why is it a bad idea to make main recursive? how is it different from any other recursive function.


The standard forbids it.
I never knew, thanks, I'll avoid it in the future.
dear OP:

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
Topic archived. No new replies allowed.