Please help,,basic c++ question

Write a C++ program that asks users for two numbers and a choice of the following operations: addition, subtraction, multiplication, division or exit the program. The program then displays the result of chosen operations on the two numbers entered or terminates. Use one function to get two numbers, separate functions for each arithmetic operations (+, -, *, /), and one function to display the output. The program should continue running until the user chooses to exit.

#include<iostream>
#include<iomanip>

using namespace std;

void Values(double &num1, double &num2)
{
cout << "Enter two valid numbers to calculate:" << endl << setw(5) << "Num1:" << setw(5);
cin >> num1;
cout << "Num2:" << setw(5);
cin >> num2;
}

void Addition(double &num1, double &num2, double &sumAdd)
{
sumAdd = num1 + num2;
}
void Subtraction(double &num1, double &num2, double &sumAdd)
{
sumAdd = num1 - num2;
}
void Multiplication(double &num1, double &num2, double &sumAdd)
{
sumAdd = num1 * num2;
}
void Division(double &num1, double &num2, double &sumAdd)
{
if (num2 != 0)
{
setprecision(5);
sumAdd = num1 / num2;
}
else
cout << "Denominator cannot be 0";
}

int main()
{
char choice;
double num1, num2, sumAdd;

Values(num1, num2);

cout << endl << endl << "Select Symbol: " << endl;
cout << "A +" << endl;
cout << "B -" << endl;
cout << "C *" << endl;
cout << "D /" << endl << endl;
cout << "Entered symbol for calculation: ";
cin >> choice;

switch (choice)
{
if (choice == 'A')
{
Addition(num1, num2, sumAdd);
}
cout << "The result is " << sumAdd << endl;
break;
if (choice == 'B')
{
Subtraction(num1, num2, sumAdd);
}
cout << "The result is " << sumAdd << endl;
break;
if (choice == 'C')
{
Multiplication(num1, num2, sumAdd);
}
cout << "The result is " << sumAdd << endl;
break;
if (choice == 'D')
{
Division(num1, num2, sumAdd);
}
cout << "The result is " << sumAdd << endl;
if (num2 != 0)
{
cout << "Denominator cannot be 0";
}
break;

}
system("pause");
return 0;
}


The code works fine but I cannot get the result,,
Last edited on
The SWITCH STATEMENTS and IF ELSE STATEMENTS both do the same exact thing.
It's upon the programmer's choice on which one to use.
But if you use both of them simultaneously, then it will cause problems. This is the reason your program is not showing you the result.
So I suggest you get rid of the if statements and just use the switch statements.

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
switch (choice)
	{
	case 'A':
			Addition(num1, num2, sumAdd);
			cout << "The result is " << sumAdd << endl;
			break;
	case 'B':
			Subtraction(num1, num2, sumAdd);
			cout << "The result is " << sumAdd << endl;
			break;
	case 'C':
			Multiplication(num1, num2, sumAdd);
			cout << "The result is " << sumAdd << endl;
			break;
	case 'D':
			Division(num1, num2, sumAdd);
			cout << "The result is " << sumAdd << endl;
			if (num2 != 0)
			{
			cout << "Denominator cannot be 0";
			}
			break;
	}
system("pause");
return 0;


In other words, copy and paste the above piece of code from line 52.
Refer to this website for more information on how to use Switch Statements.
http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm

Hope that helps!
Last edited on
@redstone266

The reason you're not getting results has nothing to do with the if statement. You're just not printing out the results.

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
void Addition(double &num1, double &num2, double &sumAdd)
{
sumAdd = num1 + num2;
// No cout of results
}
void Subtraction(double &num1, double &num2, double &sumAdd)
{
sumAdd = num1 - num2;
// And again, no cout of results
}

void Multiplication(double &num1, double &num2, double &sumAdd)
{
sumAdd = num1 * num2;
// Or here
}
void Division(double &num1, double &num2, double &sumAdd)
{
if (num2 != 0)
{
setprecision(5);
sumAdd = num1 / num2;
// No cout of results here, either
}
else
cout << "Denominator cannot be 0";
}


And lastly, since you declared the functions as void, nothing would be returned.
@newbie999
newbiee999 wrote:
The SWITCH STATEMENTS and IF ELSE STATEMENTS both do the same exact thing.


Not to be nitpicky, but this is incorrect. Not only are they often translated into machine code differently (barring the optimizer doing something clever), but the cases in switches are a lot more restrictive than the conditions in if-else chains. For instance, have you tried using a variable as one of the case conditions?

@whitenite1
The arguments are passed in by reference here. I would argue that it makes more sense to return the result, sure, but the result isn't lost, rather the sumAdd variable in main, having been passed by reference to the function, is changed, and that is then printed out back in main.

-Albatross
@TheRabbitologist

Didn't really notice the reference passing.

@redstone266

Figured out the problem. You're not using cases in the switch.
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
switch (choice)
	{
	case 'A':
		{
			Addition(num1, num2, sumAdd);
			cout << "The result is " << sumAdd << endl;
		}
		
		break;
	case  'B':
		{
			Subtraction(num1, num2, sumAdd);
			cout << "The result is " << sumAdd << endl;
		}
		
		break;
	case 'C':
		{
			Multiplication(num1, num2, sumAdd);
			cout << "The result is " << sumAdd << endl;
		}
		
		break;
	case 'D':
		{
			if (num2 == 0)
			{
				cout << "Denominator cannot be 0";
			}
			else
			{
				Division(num1, num2, sumAdd);
				cout << "The result is " << sumAdd << endl;
			}
		}
              break;
	}
Topic archived. No new replies allowed.