Help me

I'm trying to make an C++ calculator but only adding numbers works!

Here's what i tried:
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
#include <iostream>

using namespace std;

int main()
{
	int b;
	int c;
	int a;
	do{
	cout << "1- Add" <<endl;
	cout << "2- Subtract" <<endl;
	cout << "3- Multiply" <<endl;
	cout << "4- Divide" <<endl;
	cout << "-------------------------------" <<endl;
	cout << "Enter Numbers: ";
	cin>>a;
	}while (a>4);
	if (a=1);
	cout << "Enter first number: " ;
	cin>>b;
	cout << "Enter second number: " ;
	cin>>c;
	cout << b+c <<endl;
	cin>>a;
	
	if (a=2);
	cout << "Enter first number: " ;
	cin>>b;
	cout << "Enter second number: " ;
	cin>>c;
	cout << b-c <<endl;
	cin>>a;
	
	if (a=3);
	cout << "Enter first number: " ;
	cin>>b;
	cout << "Enter second number: " ;
	cin>>c;
	cout << b*c <<endl;	cin>>a;
	
	if (a=4);
	cout << "Enter first number: " ;
	cin>>b;
	cout << "Enter second number: " ;
	cin>>c;
	cout << b/c <<endl;
	return 0;
}

I want every calculation to work!
P.S.
I made this script based on this cmd script someone i know made but he deleted it from github, but anyway, here's the script:
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
69
70
71
72
73
74
75
@ECHO OFF
chcp 65001>nul
title Calculator By Memer
mode 60,20
:START
cls
echo    1 = Add
echo[
echo    2 = Subtract
echo[
echo    3 = Divide
echo[
echo    4 = Multiply
echo ——————————————————
echo[
set CHOICE=0
set A=0
set B=0
set C=0
set /P CHOICE="> "
IF %CHOICE%==1 goto PLU
IF %CHOICE%==2 goto MIN
IF %CHOICE%==3 goto DIV
IF %CHOICE%==4 goto MUL
echo Invalid Choice
pause
goto START
:PLU
echo[
echo First Number
set /P A="> "
echo[
echo Second Number
set /P B="> "
set /A C=A+B
echo ——————————————————————————————————————————————————
echo %A% Plus %B% Is %C%
pause
goto START
:MIN
echo[
echo First Number
set /P A="> "
echo[
echo Second Number
set /P B="> "
set /A C=A-B
echo ——————————————————————————————————————————————————
echo %A% Minus %B% Is %C%
pause
goto START
:DIV
echo[
echo First Number
set /P A="> "
echo[
echo Second Number
set /P B="> "
set /A C=A/B
echo ——————————————————————————————————————————————————
echo %A% Divided By %B% Is %C%
pause
goto START
:MUL
echo[
echo First Number
set /P A="> "
echo[
echo Second Number
set /P B="> "
set /A C=A*B
echo ——————————————————————————————————————————————————
echo %A% Times %B% Is %C%
pause
goto START
1. Your if statements at lines 19, 27, 36 and 42 are royally borked. You want to check for equality (==) instead of doing an assignment (=). Assignments are always true.

2. You are prematurely terminating the if statements with a semi-colon. Remove them.

3. Without enclosing the code you want to execute in curly braces blocks when an if is true only the first following line will be run. All the other lines will ALWAYS BE RUN!

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

int main()
{
   int a;

   do
   {
      std::cout << "1- Add\n";
      std::cout << "2- Subtract\n";
      std::cout << "3- Multiply\n";
      std::cout << "4- Divide\n";
      std::cout << "-------------------------------\n";
      std::cout << "Enter Operation: ";
      std::cin >> a;
   } while (a > 4);

   int b;
   int c;

   if (a == 1)
   {
      std::cout << "Enter first number: ";
      std::cin >> b;
      std::cout << "Enter second number: ";
      std::cin >> c;
      std::cout << b + c << '\n';
   }

   if (a == 2)
   {
      std::cout << "Enter first number: ";
      std::cin >> b;
      std::cout << "Enter second number: ";
      std::cin >> c;
      std::cout << b - c << '\n';
   }

   if (a == 3)
   {
      std::cout << "Enter first number: ";
      std::cin >> b;
      std::cout << "Enter second number: ";
      std::cin >> c;
      std::cout << b * c << '\n';
   }

   if (a == 4)
   {
      std::cout << "Enter first number: ";
      std::cin >> b;
      std::cout << "Enter second number: ";
      std::cin >> c;
      std::cout << b / c << '\n';
   }
}
1- Add
2- Subtract
3- Multiply
4- Divide
-------------------------------
Enter Operation: 5
1- Add
2- Subtract
3- Multiply
4- Divide
-------------------------------
Enter Operation: 3
Enter first number: 4
Enter second number: 8
32

You are duplicating a lot of code, requesting the user enter the two numbers, that could be shoved into functions.

Plus, and this is a biggie. What happens if the user enters zero as the second number for division? *BOOM!* Dividing by zero is WRONG!

Instead of having a bunch of if statements you could use a switch.
https://www.learncpp.com/cpp-tutorial/switch-statement-basics/
&
https://www.learncpp.com/cpp-tutorial/switch-fallthrough-and-scoping/
A rather clunky, quick and dirty rewrite using a switch and some testing to not divide by zero:
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
69
70
71
72
73
74
75
76
#include <iostream>

int get_1st_num();
int get_2nd_num();

int main()
{
   int a;

   do
   {
      std::cout << "1- Add\n";
      std::cout << "2- Subtract\n";
      std::cout << "3- Multiply\n";
      std::cout << "4- Divide\n";
      std::cout << "-------------------------------\n";
      std::cout << "Enter Operation: ";
      std::cin >> a;
   } while (a < 1 || a > 4);

   int b;
   int c;

   switch (a)
   {
   case 1:
      b = get_1st_num();
      c = get_2nd_num();
      
      std::cout << b << " + " << c << " = " << b + c << '\n';
      break;

   case 2:
      b = get_1st_num();
      c = get_2nd_num();

      std::cout << b << " - " << c << " = " << b - c << '\n';
      break;

   case 3:
      b = get_1st_num();
      c = get_2nd_num();

      std::cout << b << " * " << c << " = " << b * c << '\n';
      break;

   case 4:
      b = get_1st_num();
      c = get_2nd_num();

      if (c == 0)
      {
         std::cout << "Can't divide by zero!!!\n";
         break;
      }

      std::cout << b << " / " << c << " = " << b / c << '\n';
      break;
   }
}

int get_1st_num()
{
   int b;
   std::cout << "Enter first number: ";
   std::cin >> b;
   return b;
}

int get_2nd_num()
{
   int c;
   std::cout << "Enter second number: ";
   std::cin >> c;
   return c;
}
1- Add
2- Subtract
3- Multiply
4- Divide
-------------------------------
Enter Operation: -5
1- Add
2- Subtract
3- Multiply
4- Divide
-------------------------------
Enter Operation: 5
1- Add
2- Subtract
3- Multiply
4- Divide
-------------------------------
Enter Operation: 4
Enter first number: 5
Enter second number: 0
Can't divide by zero!!!

This could be streamlined more, if I wanted to devote more time to refactoring it.
Possibly something like:

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

int getNum(const char* str) {
	int a {};

	std::cout << str;
	std::cin >> a;
	return a;
}

int main() {
	while (true) {
		int a {};

		do {
			std::cout << "\n1- Add\n";
			std::cout << "2- Subtract\n";
			std::cout << "3- Multiply\n";
			std::cout << "4- Divide\n";
			std::cout << "0 - Exit\n\n";

			a = getNum("Enter option: ");
		} while ((a < 0 || a > 4) && (std::cout << "Invalid option\n"));

		if (!a)
			return 0;

		const auto b { getNum("Enter first number: ") };
		const auto c { getNum("Enter second number: ") };

		switch (a) {
			case 1:
				std::cout << b << " + " << c << " = " << b + c << '\n';
				break;

			case 2:
				std::cout << b << " - " << c << " = " << b - c << '\n';
				break;

			case 3:
				std::cout << b << " * " << c << " = " << b * c << '\n';
				break;

			case 4:
				if (c == 0)
					std::cout << "Can't divide by zero!!!\n";
				else
					std::cout << b << " / " << c << " = " << b / c << '\n';

				break;
		}
	}
}


Note that getNum() is very simplistic and if anything other than an int number is entered then there will be problems! To have getNum() correctly deal with any type of input (ie non-int) then it will need to be much more complex.

Also note that division here is integer revision. eg 6 / 4 will give 1 and 4 / 7 will give 0.
Last edited on
i once did a c++ copy-paste but realized that's not it, i see literally everyone has a different style of programming.
because c++ copy-pasting isn't learning, i want to learn, not copy and paste lines of code!

EDIT: George P, the links you sent in the post are also different than my programming style!
Last edited on
For on-line learning, the best free site is https://www.learncpp.com/

For a book I would recommend Beginning C++ From Novice to Professional
https://www.amazon.co.uk/Beginning-C-20-Novice-Professional/dp/1484258835/ref=sr_1_3


everyone has a different style of programming


Given 6 programmers, you'll get at least 9 different programs!

i want to learn, not copy and paste lines of code!


As part of learning, copy and pasting code can be useful providing that you examine and understand the code. Note though, that there is much 'bad' C++ code on the internet. Not everything there is 'good' code.

Also note that over the years the C++ language has evolved. There was the ANSI C++ version from pre 1998. Then came C++98, C++03, C++11, C++14, C++17, C++20 and now C++23 is coming later this year. C++11 and following are often called 'modern C++' because of the great changes that occurred in this version compared it the previous version. How things were coded with C++98 are likely to be different from how they would be coded using the latest version of C++.

Unless you have the knowledge, you can't really determine whether code found on the web is 'good' or 'bad', 'old' or 'modern' - or even works as suggested.

If you produce code and post it here then we'll provide advice and guidance. Same if you have a C++ question.

Enjoy and have fun!

I found the problem, it is that the Operation goes from 1 to 4 whatever number you enter.
EDIT: seeplus, I use C++98
Last edited on
I use C++98


Then I really, really recommend that you upgrade to a C++20 compatible compiler.

Which compiler/os do you currently use?
I found the problem


An original problem is as the 1st point made by George in his first post here.

In C++, == is for equality test. = means assignment. So

 
if (a=1);


means assign 1 to a. If the result is true (always yes as a will always be 1 and the result of the assignment will be 1), then execute the following statement. But the following statement is null as the if statement is terminated by a ; with no statement before. Hence irrespective of the result of the if statement, the statements following are always executed. Hence the addition will always be performed.

Then for some reason you're asking for a to be input again. If a value is entered here, then subtraction will be performed. And again with a etc etc.

AlanHudik wrote:
the links you sent in the post are also different than my programming style!

A lot of your "style" is likely based on very outdated ideas and usages. C++98 is an ancient, deprecated standard. It is 34 years old.*

At a bare minimum you should use C++11. Better would be C++20 since that is the current language standard.

There is a nice list of "Do's and Don'ts" suggestions (not requirements) for writing good, consistent C++ code. Written by people who live, eat and breathe C++. One is the guy who created C++, Bjarne Stroustrup.

The C++ Code Guidelines:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines

Using a newer language standard would allow you to enter the 21st Century and stop using bear skins and stone knives to program.

If you are looking to be employed as a programmer you really do need to learn current practices.

From the C++ Core Guidelines:
This document is a set of guidelines for using C++ well. The aim of this document is to help people to use modern C++ effectively. By “modern C++” we mean effective use of the ISO C++ standard (currently C++20, but almost all of our recommendations also apply to C++17, C++14 and C++11). In other words, what would you like your code to look like in 5 years’ time, given that you can start now? In 10 years’ time?

Another suggestion is learning how to ask for help with C++ code:
http://www.gregcons.com/KateBlog/HowToAskForCCodingHelp.aspx

https://github.com/selfteaching/How-To-Ask-Questions-The-Smart-Way/blob/master/How-To-Ask-Questions-The-Smart-Way.md

*My coding style over the years has evolved. A lot. For the better. And it continues to evolve. I started learning C++ before C++98 was standardized.

I look at old code I wrote months and years ago and cringe.

If you think this is being unfairly harsh, think again. If I truly didn't want to help I could have just ignored you and not bothered with this follow-up.
Topic archived. No new replies allowed.