-2

I'm very new to C++. I started teaching myself C++ last week, and up until now, I haven't had any real problems. I use Microsoft Visual Studio 2017, and I'm having problems with if and else statements. You see, I'm making this very limited calculator. Basically, the program gives you the 4 basic mathematical operations, and you choose which operation you want to use to calculate by entering either 1, 2, 3, or 4. Then, it runs another program which you can then calculate. (e.g.: 2 is for subtraction, if you enter 2, it will run the subtraction calculator) Here's the code for the program.

#include "stdafx.h"
#include "iostream"
using namespace std;

int main(){
int add = 1; 
int sub = 2; 
int mul = 3; 
int div = 4; 
cout << "Extremely Limited C++ Calculator: Enter a number between one and 
four to start calculating and press enter." 
    << '\n' 
    << "LEGEND" 
    << '\n' << "1 = Addition"
    << '\n' << "2 = Subtraction"
    << "\n" << "3 = Multiplication"
    << '\n' << "4 = Division" 
    << '\n' << "Operation: "; 
if (cin >> add) {
system("start C:\\CalculatorApps\\addition.exe");
return 0;
}
if (cin >> sub) {
system("start C:\\CalculatorApps\\subtraction.exe");
return 0; 
}
if (cin >> mul) {
system("start C:\\CalculatorApps\\multiplication.exe");
return 0; 
}
if (cin >> div) {
system("start C:\\CalculatorApps\\division.exe");
return 0; 
}
}

So I have the addition.exe and subtraction.exe done, but the problem is that no matter what number I enter, it will always run addition.exe. In the subtraction calculator, I experimented having the user choose if they wanted to subtract more than 2 numbers, but that also didn't end up working because it was ignoring if statements. I also at one point had an else statement on both the subtraction calculator and the main program that takes you to the calculators that displayed text reading that the number they entered was not a valid choice and to enter a valid choice, but even that was ignored by the program. Now, maybe I didn't look hard enough on the internet, but I couldn't find one that helped me out. If you know the answer, please tell me but in words that I can understand (I am new after all), or please link me to another question that has been answered that will solve my question. Thank you in advanced!

  • 1
    Your problem is not your if/then, it is your (wrong) use of `cin`. You use `cin >> var` to assign input to a variable; `cin` by itself is not a variable. So, use `cin` with the extraction operator to assign to a var, and compare that to the input you expect. – Joe Jul 11 '17 at 01:31
  • `if(cin >> div)` tells you if the input was successful. It does not tell you what was input. – Galik Jul 11 '17 at 01:36
  • 1
    I recommend working methodically through one of the recommended books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Galik Jul 11 '17 at 01:38
  • An `if` statement is never ignored. You must be misunderstanding what you are seeing. To improve the question, describe accurately what you typed and what you saw and explain how this differed from your expectation. – M.M Jul 11 '17 at 02:27
  • @Joe Ah, I was under the assumption that those if statements I wrote checked to see if the user input was equal to the value of the variables which would run the programs. I've gone ahead and changed the program based on your recommendation, and it now functions as intended. Thank you so much! – Christian R. Jul 12 '17 at 00:37

1 Answers1

0

cin is an object of class istream that represents the standard input stream. It corresponds to the cstdio stream stdin. The operator >>overload for streams return a reference to the same stream. The stream itself can be evaluated in a boolean condition to true or false through a conversion operator.

cin provides formatted stream extraction. The operation cin >> x;

where "x" is an int will fail if a non-numeric value is entered. So:

if(cin>>x)

will return false if you enter a letter rather than a digit.

As you are always entering a digit, your first statement if(cin>>sum) is always true. So, your other statements are skipped.

Tanuj Yadav
  • 1,259
  • 13
  • 21