Help Fixing Program

closed account (yqGNwA7f)
Ok, basically I am making a console arithmetic calculator just for learning, I am new. I can't seem to figure out why no matter what method I use, it adds it even if I chose to multiply, please look at the code and tell me how to fix it, or help me in any way you can, all help is appreciated. Thanks in advance.

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
#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include <cstdlib>
#include <cstdio>
// using namespace std;

int num1;			// first int
int num2;			// second int

int method;			// method e.g. [+, -, *, /]

int add;			// addition [+]
int sub;			// subtraction [-]
int mul;			// multiplication [*]
int divide;			// dividesion [/]
int mod;			// modulous [%]

/*
int quo;			// answer to division [/]
int sum;			// answer to addition [+]
int difference;		// answer to subtraction [-]
int product;		// answer to multiplication[*]
*/

int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << "Enter first integer or float value: " << std::endl;
	std::cin >> num1;

	std::cout << "Enter second integer or float value: " << std::endl;
	std::cin >> num2;

	std::cout << "Enter method: add, sub, mul, divide, mod" << std::endl;
	std::cin >> method;

	if( method == add )
	{
		std::cout << "The answer is: " << num1 + num2 << std::endl;
	}
	else if( method != add && method == sub )
	{
		std::cout << "The answer is: " << num1 - num2 << std::endl;
	}
	else if( method != add || sub && method == mul )
	{
		std::cout << "The answer is: " << num1 * num2 << std::endl;
	}
	else if( method != add || sub || mul && method == divide )
	{
		std::cout << "The answer is: " << num1 / num2 << std::endl;
	}
	else if( method != add || sub || mul || divide && method == mod )
	{
		std::cout << "The answer is: " << num1 % num2 << std::endl;
	}
	else
	{
		std::cout << "Unknown function, please use [ +, -, *, / ]" << std::endl;
	}
	
	system("PAUSE");
}
Few problems here. First of all...

It's your usage of ||. I believe (could be wrong here mind) that you have to be explicit each time you use || so you would have to do method != add || method != sub, etc...

But you don't need the != part either.

Just use

else if(method == sub ) instead (for example)


BUT!

I'd like you to tell me what sub,add, etc actually are? They haven't been set to anything so the statement if (method == add) for example doesn't actually mean anything.
You could assign add,sub, etc values such as 1,2,3 and use
if (method == 1) for example.

Or you could use strings and have

string method;

std::cout << "Enter method: add, sub, mul, divide, mod" << std::endl;
getline(std::cin, method);


if (method == "add")
{
// do some addition.
}
Last edited on
closed account (yqGNwA7f)
Originally I had it as else if(method == sub ) but I thought maybe it'd fix the program, I don't really understand alot of C++. Just basic math, but how do I make it so when I enter a method when it asks for (e.g. add, sub, mul, divide, mod) that it returns the two numbers either added, multiplied etc depending on what method was entered by the user?
set values of add, sub, etc...

So have,

int add = 1;
int sub = 2;
int mul = 3;
int divide = 4;
int mod = 5;

Then in your loop put, for example...

if( method == 1 )
{
// do some addition.
}
else if (method == 2)
{
// do some subtraction
}
closed account (yqGNwA7f)
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
#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include <cstdlib>
#include <cstdio>

int num1;			// first int
int num2;			// second int

int method;			// method e.g. [+, -, *, /]

int add = 1;		// addition
int sub = 2;		// subtraction
int mul = 3;		// multiplication
int divide = 4;		// division
int mod = 5;		// modulous

/*
int quo;			// answer to division [/]
int sum;			// answer to addition [+]
int diff;		// answer to subtraction [-]
int prdct;		// answer to multiplication[*]
*/

int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << "Enter first integer: " << std::endl;
	std::cin >> num1;

	std::cout << "Enter second integer: " << std::endl;
	std::cin >> num2;

	std::cout << "Enter method: add (1), sub (2), mul (3), divide (4), mod (5)" << std::endl;
	std::cin >> method;

	if( method == 1 )
	{
		std::cout << "The answer is: " << num1 + num2 << std::endl;
	}
	else if( method == 2 )
	{
		std::cout << "The answer is: " << num1 - num2 << std::endl;
	}
	else if( method == 3 )
	{
		std::cout << "The answer is: " << num1 * num2 << std::endl;
	}
	else if( method == 4 )
	{
		std::cout << "The answer is: " << num1 / num2 << std::endl;
	}
	else if( method == 5 )
	{
		std::cout << "The answer is: " << num1 % num2 << std::endl;
	}
	else
	{
		std::cout << "Unknown function, please use [ +, -, *, / ]" << std::endl;
	}
	
	system("PAUSE");
}


This code works, thank you so much for your help!
Topic archived. No new replies allowed.