Need Help ASAP with a Loop

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  // Calculator, by Keegan Mathur

// Include the iostream library
#include <iostream>

//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
closed account (o3hC5Di1)
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:

1
2
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;
}
// 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...
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;
}
// 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");


}
Looks like its doing what you asked.
1
2
3
4
    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?

1
2
3
4
5
6
7
8
9
10

cout << "How many problems would you like to do?";
cin >> somevar;

while(somevar > 0){

//Code

somevar--;
}
closed account (o3hC5Di1)
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).

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.

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.