MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Become a Premium Member and unlock a new, free course in leading technologies each month.

Solved

Posted on 2013-01-05

Hello everyone, I recently started learning about the C++ Programming Language through online tutorials and would like some help with a basic program that I have written. The program is a simple calculator that will perform a desired operation and then ask for the user's input on what to do next. Here is my code below:

#include <cstdlib>

#include <iostream>

using namespace std;

float addition(float value1, float value2)

{

return value1 + value2;

}

float subtraction (float value1, float value2)

{

return value1 - value2;

}

float multiplication (float value1, float value2)

{

return value1 * value2;

}

float division (float value1, float value2)

{

return value1 / value2;

}

int main(int argc, char *argv[])

{

float number1;

float number2;

int choice;

cout << "Would you like to: \n\nAdd (1) \nSubtract (2) \nMultiply (3) \nDivide (4) \nQuit (5) \n" << endl;

cout << "Choice: ";

cin >> choice;

while (true)

{

switch (choice)

{

case 1:

{

//Addition

cout << "You are adding two numbers, enter the first number. \n Number 1: ";

cin >> number1;

cout << "Enter the second number. \n Number 2: ";

cin >> number2;

cout << "The answer is: " << addition(number1, number2) << endl;

system("PAUSE");

}

case 2:

{

//Subtraction

cout << "You are subtracting two numbers, enter the first number. \n Number 1: ";

cin >> number1;

cout << "Enter the second number. \n Number 2: ";

cin >> number2;

cout << "The answer is: " << subtraction(number1, number2) << endl;

system("PAUSE");

}

case 3:

{

//Multiplication

cout << "You are multiplying two numbers, enter the first number. \n Number 1: ";

cin >> number1;

cout << "Enter the second number. \n Number 2: ";

cin >> number2;

cout << "The answer is: " << multiplication(number1, number2) << endl;

system("PAUSE");

}

case 4:

{

//Division

cout << "You are dividing two numbers, enter the first number. \n Number 1: ";

cin >> number1;

cout << "Enter the second number. \n Number 2: ";

cin >> number2;

cout << "The answer is: " << division(number1, number2) << endl;

system("PAUSE");

}

case 5:

{

cout << "Goodbye!";

system("PAUSE");

return 0;

}

default:

cout << "Please enter a value between 1 and 5";

}

}

system("PAUSE");

return 0;

}

The "quit" option that I have put in works fine, but the other cases have issues. All of my cases except the default case will start with the desired operation, but will then immediately jump over to the following operation afterwards (i.e. pressing 1 will start the addition, but will go straight to asking for subtraction when it finishes). The default case does not loop back to the question that is asked at the beginning of the program. Any help is appreciated.

#include <cstdlib>

#include <iostream>

using namespace std;

float addition(float value1, float value2)

{

return value1 + value2;

}

float subtraction (float value1, float value2)

{

return value1 - value2;

}

float multiplication (float value1, float value2)

{

return value1 * value2;

}

float division (float value1, float value2)

{

return value1 / value2;

}

int main(int argc, char *argv[])

{

float number1;

float number2;

int choice;

cout << "Would you like to: \n\nAdd (1) \nSubtract (2) \nMultiply (3) \nDivide (4) \nQuit (5) \n" << endl;

cout << "Choice: ";

cin >> choice;

while (true)

{

switch (choice)

{

case 1:

{

//Addition

cout << "You are adding two numbers, enter the first number. \n Number 1: ";

cin >> number1;

cout << "Enter the second number. \n Number 2: ";

cin >> number2;

cout << "The answer is: " << addition(number1, number2) << endl;

system("PAUSE");

}

case 2:

{

//Subtraction

cout << "You are subtracting two numbers, enter the first number. \n Number 1: ";

cin >> number1;

cout << "Enter the second number. \n Number 2: ";

cin >> number2;

cout << "The answer is: " << subtraction(number1, number2) << endl;

system("PAUSE");

}

case 3:

{

//Multiplication

cout << "You are multiplying two numbers, enter the first number. \n Number 1: ";

cin >> number1;

cout << "Enter the second number. \n Number 2: ";

cin >> number2;

cout << "The answer is: " << multiplication(number1, number2) << endl;

system("PAUSE");

}

case 4:

{

//Division

cout << "You are dividing two numbers, enter the first number. \n Number 1: ";

cin >> number1;

cout << "Enter the second number. \n Number 2: ";

cin >> number2;

cout << "The answer is: " << division(number1, number2) << endl;

system("PAUSE");

}

case 5:

{

cout << "Goodbye!";

system("PAUSE");

return 0;

}

default:

cout << "Please enter a value between 1 and 5";

}

}

system("PAUSE");

return 0;

}

The "quit" option that I have put in works fine, but the other cases have issues. All of my cases except the default case will start with the desired operation, but will then immediately jump over to the following operation afterwards (i.e. pressing 1 will start the addition, but will go straight to asking for subtraction when it finishes). The default case does not loop back to the question that is asked at the beginning of the program. Any help is appreciated.

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

- Help others & share knowledge
- Earn cash & points
- Learn & ask questions

1 Comment

Yes, you have to terminate each 'case:' branch with a 'break' statement, i.e.

```
while (true)
{
switch (choice)
{
case 1:
{
//Addition
cout << "You are adding two numbers, enter the first number. \n Number 1: ";
cin >> number1;
cout << "Enter the second number. \n Number 2: ";
cin >> number2;
cout << "The answer is: " << addition(number1, number2) << endl;
system("PAUSE");
}
break; // <-----
case 2:
{
//Subtraction
cout << "You are subtracting two numbers, enter the first number. \n Number 1: ";
cin >> number1;
cout << "Enter the second number. \n Number 2: ";
cin >> number2;
cout << "The answer is: " << subtraction(number1, number2) << endl;
system("PAUSE");
}
break; // <-----
case 3:
{
//Multiplication
cout << "You are multiplying two numbers, enter the first number. \n Number 1: ";
cin >> number1;
cout << "Enter the second number. \n Number 2: ";
cin >> number2;
cout << "The answer is: " << multiplication(number1, number2) << endl;
system("PAUSE");
}
break; // <-----
case 4:
{
//Division
cout << "You are dividing two numbers, enter the first number. \n Number 1: ";
cin >> number1;
cout << "Enter the second number. \n Number 2: ";
cin >> number2;
cout << "The answer is: " << division(number1, number2) << endl;
system("PAUSE");
}
break; // <-----
case 5:
{
cout << "Goodbye!";
system("PAUSE");
return 0;
}
break; // <-----
default:
cout << "Please enter a value between 1 and 5";
}
}
```

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Earn Certification

HTML5 Specialist - Certification

Free withPremium

Course of the Month10 days, 15 hours left to enroll

Join the community of 500,000 technology professionals and ask your questions.