//Calculator by Me
//Includes the iostream library
#include <iostream>
//Use the standard namespace
usingnamespace std;
void main ( )
{ //Declares the Variables
float Number_1;
float Number_2;
float Result;
int Which_Calculation;
//Give instructions
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;
cin >> Which_Calculation;
//Get numbers
cout << "Please enter the first number" << endl;
cin >> Number_1;
cout << "Please enter the second number" << endl;
cin >> Number_2;
if (Which_Calculation == 1)
{
//Calculate the result
Result = Number_1 + Number_2;
}
if (Which_Calculation == 2)
{
//Calculate the result
Result = Number_1 - Number_2;
}
if (Which_Calculation == 3)
{
//Calculate the result
Result = Number_1 * Number_2;
}
if (Which_Calculation == 4)
{
//Calculate the result
Result = Number_1 / Number_2;
}
//Print the answer is...
cout << "The answer is..." << endl;
//Print the result
cout << Result << endl;
system ("PAUSE");
}
I have to add one "While" loop to this code, and also make it so that when i ask the user "Would you like to do another task?" the person clicks "1" the program will restart, and if the user hits "2" the program will close. any suggestions how to do that? thanks in advance!
I think you mean press 1 not click one, as the program is a console application. And please, DON'T declare main as a void! Declare it as an int, and return 0 at the end to signify that it worked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cout << "Would you like to do another task?" << endl; // ask
int reply; // the users reply
cin >> reply; // get the input
switch(reply) // in case you're not familiar with switch statements, its a short way of doing loads of if statements about the same variable
{
case 1: // if they typed 1...
{
main(); // repeat the main function again
}
case 2: // if they typed 2...
{
return 0; // exit the program, and tell it that nothing went wrong
}
}
I have to add one "While" loop to this code
Does it have to be any old while loop, or does it have to do something?
ok, and yes, my teacher says i have to have a "while" loop in it. We haven't learned about "switch" yet, so maybe i should stick with the "if" statements for now...
while (Guess != Secret_Number)
{
//This code repeats until the condition is no longer true
// Get the user's guess
cout << "Guess the secret number." << endl;
cin >> Guess;
//If the guess is too high, tell the user
if (Guess > Secret_Number)
{
cout <<"Your guess is too high." << endl;
}
// If the guess is too low, tell the user
if (Guess < Secret_Number)
{
cout <<"Your guess is too low." << endl;
}
//If the user guessed the right number, congratulate them
if (Guess == Secret_Number)
{
cout <<"Good job! You guessed the secret number!" << endl;
}
}
that's what one of my "while" loops looked like during another program i created... so something similar to that if possible. so switch System ("PAUSE") with return 0;?
Instead of void main have main as an integer: int main. This way you can use return to give 0 for success or 1 for failure. So return 0; needs to be at the end of your program.
You could insert the while loop before line 17, which is:
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;
You'll also want to include a check number to be a condition for the while loop (should be initialized to 1).
At the end of the loop, ask the user if they would like to enter another calculation (1 for another, 2 for exit)
Again, declare main as an int, and return 0 after the while loop.
It would look like:
1 2 3 4 5 6 7 8 9
int check = 1;
while (check == 1)
{
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide"
//the rest of your program
}
return 0;
//Calculator by me
//Includes the iostream library
#include <iostream>
//Use the standard namespace
usingnamespace std;
int main ( )
{ //Declares the Variables
float Number_1;
float Number_2;
float Result;
int Which_Calculation;
int check = 1;
while (check == 1)
{
//Give instructions
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;
cin >> Which_Calculation;
//Get numbers
cout << "Please enter the first number" << endl;
cin >> Number_1;
cout << "Please enter the second number" << endl;
cin >> Number_2;
if (Which_Calculation == 1)
{
//Calculate the result
Result = Number_1 + Number_2;
}
if (Which_Calculation == 2)
{
//Calculate the result
Result = Number_1 - Number_2;
}
if (Which_Calculation == 3)
{
//Calculate the result
Result = Number_1 * Number_2;
}
if (Which_Calculation == 4)
{
//Calculate the result
Result = Number_1 / Number_2;
}
//Print the answer is...
cout << "The answer is..." << endl;
//Print the result
cout << Result << endl;
cout << "Would you like to do another task?" << endl; // ask
int reply; // the users reply
cin >> reply; // get the input
switch(reply)
{
case 1: // if they typed 1...
{
main(); // repeat the main function again
}
case 2: // if they typed 2...
{
return 0; // exit the program.
}
}
}
cin.get();
return 0;
}
This should work. Using the reply at the end with switch would be redundent as you already have "check" which initiates the loop. Once "check" is not equal to 1 the program skips the loop, thus ending the program.
//Calculator by me
//Includes the iostream library
#include <iostream>
//Use the standard namespace
usingnamespace std;
int main ( )
{ //Declares the Variables
float Number_1;
float Number_2;
float Result;
int Which_Calculation;
int check = 1;
while (check == 1)
{
//Give instructions
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;
cin >> Which_Calculation;
//Get numbers
cout << "Please enter the first number" << endl;
cin >> Number_1;
cout << "Please enter the second number" << endl;
cin >> Number_2;
if (Which_Calculation == 1)
{
//Calculate the result
Result = Number_1 + Number_2;
}
if (Which_Calculation == 2)
{
//Calculate the result
Result = Number_1 - Number_2;
}
if (Which_Calculation == 3)
{
//Calculate the result
Result = Number_1 * Number_2;
}
if (Which_Calculation == 4)
{
//Calculate the result
Result = Number_1 / Number_2;
}
//Print the answer is...
cout << "The answer is..." << endl;
//Print the result
cout << Result << endl;
cout << "Would you like to do another task? (1 for yes, 2 for no)" << endl; // ask
cin >> check; // get the input
}
cin.get();
return 0;
}
Here are a few changes I might suggest, if I may make an input.
@Lines 63-72: Believe it or not, none of these are necessary. In fact, they kill the purpose of your while loop.
@Line 58: Maybe output some sort of indication that the user has to type 1 to repeat the loop?
@Line 60: You don't need this. You'll see why next line.
@Line 61: Try cin >> check; This way, you not only make use of that variable, but you achieve virtually the same results, and your code is shorter and better optimized for memory usage before the compiler does anything to it.
EDIT:
mookial beat me to it, but I would like to ask in the future that he/she doesn't give out code solutions. :)
-Albatross
@programmer47:
That switch is somewhat wrong, because while it seems like it would work I think you thought switch works a different way that it actually does. Just a hunch. Also, I would suggest avoiding recursion to repeat main() unless you're 100% certain you know what you're doing. Here, you seemed only 99% certain. :/ http://msdn.microsoft.com/en-us/library/k0t5wee3(VS.80).aspx
EDIT: mookial beat me to it, but I would like to ask in the future that he/she doesn't give out code solutions. :)
Ahh! Sorry, I knew I shouldn't have gave the answer but I realized this a little to late. I'm a new programmer as well so I know the OP's pain =P So from one newbie to another I wanted to help.
Even so, your input is probably far better than mine.
//Calculator by me
//Includes the iostream library
#include <iostream>
//Use the standard namespace
usingnamespace std;
int main ( )
{ //Declares the Variables
float Number_1;
float Number_2;
float Result;
int Which_Calculation;
int check = 1;
while (check == 1)
{
//Give instructions
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide" << endl;
cin >> Which_Calculation;
//Get numbers
cout << "Please enter the first number" << endl;
cin >> Number_1;
cout << "Please enter the second number" << endl;
cin >> Number_2;
if (Which_Calculation == 1)
{
//Calculate the result
Result = Number_1 + Number_2;
}
if (Which_Calculation == 2)
{
//Calculate the result
Result = Number_1 - Number_2;
}
if (Which_Calculation == 3)
{
//Calculate the result
Result = Number_1 * Number_2;
}
if (Which_Calculation == 4)
{
//Calculate the result
Result = Number_1 / Number_2;
}
//Print the answer is...
cout << "The answer is..." << endl;
//Print the result
cout << Result << endl;
cout << "Would you like to do another task? Press 1 if you would, press 2 if you would like to exit the program." << endl; // ask
cin >> check; // get the input
}
}
int cin,get();
return 0;
yeah, i just took it off again... and returned all those lines back to normal... it looks like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
cout << "Would you like to do another task? Press 1 if you would, press 2 if you would like to exit the program." << endl; // ask
cin >> check; // get the input
}
}
cin.get();
return 0;
}
and when i debugged it, here are the errors.
Error 4 error C2059: syntax error : '}' 71
Error 6 error C2059: syntax error : '}' 71
Error 3 error C2059: syntax error : 'return' 69
Error 1 error C2143: syntax error : missing ';' before '.' 67
Error 5 error C2143: syntax error : missing ';' before '}' 71
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 67