Calculator shows only last ElseIf

Hi people. By posting in Beginner section you can see i just started learning C++. Ofc i use this site's tutorial for learning, i think it's great so i bookmarked it. I am a waiter in night club, but i have passion for computers so i want to try this. I have read "Before post MUST DO" but to tell you the truth as a newbie i pretty much don't know what to search for. Anyway, i made a simple calculator that can make only 1 operation, now i want User to input Digit1, Digit2 and Operation as a letter. I finished code and then i tried to run application. Shows no errors either warnings, but it doesn't perform operation requested by the user. It should go like this: Input 2 digits, A,B,C,D for operation (+,-,*,/) and then to calculate. Since it would be possible that user enter something other instead of declared letters, i did error handling to make cout showing that input is invalid. It's just a start off. So if someone can help me with this, code is at the end of post. Sorry for bad English and if i made post hard to understand. All the best
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

#include <iostream>
using namespace std;
// Thanks to High Definition from HF for learning me and to cplusplus.com
int main()
{
int a, b, c, d, e, f, g;
char h;
cout << "Enter number 1 : ";
cin >> e;
cout << "Enter number 2 : ";
cin >> f;
cout << "(choose) Operation to perform:\n Addiction:A \n Substraction: B\n Multiplication: C\n Division: D\n";

cin >> h;

if('h' == a)
{
    g=e+f;
    cout << g;
}
else if('h' == b)
{
    g=e-f;
    cout << g;

}
else if('h' == c)
{
    g=e*f;
    cout << g;
}
else if('h' == d)
{

    g=e/f;
    cout << g;
}
else ('h' != a || 'h' != b || 'h' != c || 'h' != d);
    cout << "Unknown character!";

return(0);

}
'h' is a character literal, not a variable...

should be h == 'a'


- edit: you don't need vars a,b,c, and d... plus what happens if f == 0 and you divide?
Last edited on
enter 2147483647 + 1 or 46341 * 46341...
Read the comments in the code below to see what you need to fix:
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
#include <iostream>
using namespace std;


int main()
{
  // I like descriptive names (or descriptive enough names.)
  int num1, num2; // You actually only needed two ints, num1 and num2.
  int answer;  // An integer to hold the answer
  char choice; // descriptive choice char!

  cout << "Enter number 1 : ";
  cin >> num1;
  cout << "Enter number 2 : ";
  cin >> num2;
  cout << "(choose) Operation to perform:\n"
       << "Addiction: (A)\n"
       << "Substraction: (B)\n"
       << "Multiplication: (C)\n"
       << "Division: (D)\n"; // This will look like your text before but now
                             // Becomes easier to see your options and what you have.
  cin >> choice;

  if(choice == 'a') // You need to set it up like this.
  // before you where comparing the literal char 'h' with integer variable a
  // This was the problem and you did it throughout
  // The correct is a character variable like "choice" compared to a character
  // literal like: 'a' 'b' ';' 'q' '=' and so on.
  {
      answer = num1 + num2;
      cout << answer << endl;
  }
  else if('h' == b) // Fix this to be like above if condition.
  {
      g=e-f;
      cout << g;

  }
  else if('h' == c)  // Fix this to be like above if condition.
  {
      g=e*f;
      cout << g;
  }
  else if('h' == d)  // Fix this to be like above if condition.
  {

      g=e/f;
      cout << g;
  }
  // Problem here. ELSE needs no conditions to evaluate. It works if all other if and else if fails.
  else //('h' != a || 'h' != b || 'h' != c || 'h' != d)
       //; <-- BAD SEMICOLON
      cout << "Unknown character!"; // This will now print if the above is fixed

  return 0;

  // This still will give errors. You need to repair what I showed you.
}
i did it differently and btw it only does the basics though no fractions or anything like that. just for fun for me.

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
#include <iostream>

int main(int argc, char * argv[])
{
	std::cout << "Welcome to Calculator 1.0" << std::endl;
	std::cout << std::endl;

	std::cout << "Enter the first number: ";
	float first = 0;
	std::cin >> first;

	std::cout << "Enter the second number: ";
	float second = 0;
	std::cin >> second;

	std::cout << std::endl << std::endl;

	std::cout << "Enter a Operation Number\n1 Addition\n2 Subtraction\n3 Multiply\n4 Divide : ";
	int response = 0;
	std::cin >> response;
	
	std::cout << std::endl;

	switch (response)
	{
	case 1:
		std::cout <<  first << " + " << second  << " = " << first + second << std::endl;
		break;

	case 2:
		std::cout <<  first << " - " << second  << " = " << first - second << std::endl;
		break;

	case 3:
		std::cout <<  first << " x " << second  << " = " << first * second << std::endl;
		break;
	
	case 4:
		if (first == 0 || second == 0)
		{
			std::cout << "Unable to divide by zero." << std::endl;
			break;
		}

		std::cout <<  first << " / " << second  << " = " << first / second << std::endl;
		break;

	default:
		std::cout << "Unknown choice of operation. " << std::endl;
		break;
	}
	
	std::cin.sync();
	std::cout << std::endl;
	std::cout << "press Enter to exit";
	getchar();
	return 0;

}


acron... you can divide 0/1, just not 1/0

easy enough to fix. oddly enough i didnt know that. thanks for telling me.
fixed.

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
#include <iostream>

int main(int argc, char * argv[])
{
	std::cout << "Welcome to Calculator 1.0" << std::endl;
	std::cout << std::endl;

	std::cout << "Enter the first number: ";
	float first = 0;
	std::cin >> first;

	std::cout << "Enter the second number: ";
	float second = 0;
	std::cin >> second;

	std::cout << std::endl << std::endl;

	std::cout << "Enter a Operation Number\n1 Addition\n2 Subtraction\n3 Multiply\n4 Divide : ";
	int response = 0;
	std::cin >> response;
	
	std::cout << std::endl;

	switch (response)
	{
	case 1:
		std::cout <<  first << " + " << second  << " = " << first + second << std::endl;
		break;

	case 2:
		std::cout <<  first << " - " << second  << " = " << first - second << std::endl;
		break;

	case 3:
		std::cout <<  first << " x " << second  << " = " << first * second << std::endl;
		break;
	
	case 4:
		if (second == 0)
		{
			std::cout << "Unable to divide by zero." << std::endl;
			break;
		}
		std::cout <<  first << " / " << second  << " = " << first / second << std::endl;
		break;

	default:
		std::cout << "Unknown choice of operation. " << std::endl;
		break;
	}
	
	std::cin.sync();
	std::cout << std::endl;
	std::cout << "press Enter to exit";
	std::getchar();
	return 0;

}
Last edited on
Topic archived. No new replies allowed.