### Need Help ASAP with a Loop

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` `````` // Calculator, by Keegan Mathur // Include the iostream library #include //Use the standard namespace using namespace 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!
Last edited on
Hi there,

You need to actually allow the user to change that variable.
Somewhere within your loop (probably at the end of it) you'll want to do something like:

 ``12`` ``````std::cout << "Enter 1 to make another calculation or any other number to quit: "; std::cin >> Guess;``````

Also, you'll want to make SecretNumber equal to 1 at the top of main():

` const int SecretNumber=1;`

Hope that helps, please do let us know if you have any further questions.

All the best,
NwN
Last edited on
// Calculator, by Keegan Mathur

// 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;
}
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...
`while (Guess != Secret_Number)`

You didn't define Guess in your second post int, float, double, etc.
Last edited on
No errors now but it just opens CMD and says "Press any key to continue..." and does not run the application
// Calculator, by Keegan Mathur

// Include the iostream library
#include <iostream>

//Use the standard namespace
using namespace std;

void main ( )
{
// Declare the variables
int Secret_Number = 1;
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;
}
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");

}
Looks like its doing what you asked.
 ``1234`` `````` int Secret_Number = 1; int Guess = 1; while (Guess != Secret_Number) { } system ("PAUSE");``````

The while condition is `false`, so execution continues at the last line of the program.
Ok, I fixed that issue by making "int Guess = 2"
How do I get the application to close itself if you dont press the 1 key?
Not sure I am following your question.

Your program exits the loop now buy the user entering a sentinel value, and main ends.

Do you want it to exit some other way?

 ``12345678910`` `````` cout << "How many problems would you like to do?"; cin >> somevar; while(somevar > 0){ //Code somevar--; }``````
Hi there,

Please note that in C++, main() returns an int:

`int main(int argc, char** argv)`

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).

 ``12345678910`` ``````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.

All the best,
NwN
Last edited on
Sorry for such a late reply, major computer issues. I got it working not sure how but its working :) thank you guys very much!
Topic archived. No new replies allowed.