// Calculator, by Keegan Mathur
// Include the iostream library
#include <iostream>
//Use the standard namespace
usingnamespace std;
void main ( )
{
// Declare the variables
int Guess = 1;
while (Guess != Secret_Number)
{
//This code repeats until the condition is no longer true
// Declare 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 am trying to get this code to loop, it was due on Tuesday and I have been back and forth with my teacher, I need the Calculator to loop if "1" is pressed and exit if any other key is pressed. PLEASE HELP ME!
// Include the iostream library
#include <iostream>
//Use the standard namespace
using namespace std;
void main ( )
{
// Declare the variables
int Secret_Number = 1;
while (Guess != Secret_Number)
{
//This code repeats until the condition is no longer true
// Declare 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;
cout << "Enter 1 to make another calculation or any other number to quit: ";
cin >> Guess;
}
system ("PAUSE");
}
Two Errors? Thank you very much though, I am out over 100 points if I dont get this in soon (would take me from an A- to a C+) and the teacher is gone...
This matters because now, in order to exit the program, you can return a number from the main function (the number zero is used to indicate successful running and exiting of the program).
1 2 3 4 5 6 7 8 9 10
int main(int argc, char** argv)
{
//variables
while (Guess == 1)
{
//loop stuff
}
return 0; //out of the loop, exit the program
}
You will need to remove system("PAUSE");, the loop keeps the console windows open for you as long as needed.