### Help me

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

Here's what i tried:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include using namespace std; int main() { int b; int c; int a; do{ cout << "1- Add" <>a; }while (a>4); if (a=1); cout << "Enter first number: " ; cin>>b; cout << "Enter second number: " ; cin>>c; cout << b+c <>a; if (a=2); cout << "Enter first number: " ; cin>>b; cout << "Enter second number: " ; cin>>c; cout << b-c <>a; if (a=3); cout << "Enter first number: " ; cin>>b; cout << "Enter second number: " ; cin>>c; cout << b*c <>a; if (a=4); cout << "Enter first number: " ; cin>>b; cout << "Enter second number: " ; cin>>c; cout << b/c <

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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````@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!

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````#include 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:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include 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:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include 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: