-2

In C++ how do you use variables in if...then... or if...else or if... statements?

Here's what I mean.

When I enter something like this to make a calculator or something:

int main()
{
  signed int a, b, c, d, e result;
  cin >> a;
  cin >> b;
  cin >> c;
  cin >> d;
  cin >> e;

  if(d=="+")
    if(e=="-")
      result = a + b - c
  cout <<result;
}

It doesn't work.

What am I doing wrong?

4

5 に答える 5

0
int main()
{
int a, b, c,result; // you don't need write signed because it's by default signed
char d,e;
  cin >> a;
  cin >> b;
  cin >> c;
  cin >> d;
  cin >> e;

if(d=='+'){ // good practice to put {}. if you'll put another line of code in the future 
  if(e=='-'){
    result = a + b - c; //you forgot statement ";" 
   }
}
cout <<result;
于 2013-02-12T00:44:18.767 に答える
0

文字を変数と比較するときは、文字を一重引用符で囲む必要があります。例えば

char mychar='a';

if (mychar=='a')

cout<<「その」;

于 2012-11-25T17:51:32.320 に答える
0

As chris says, you can't compare a string literal to an integer.

If you want to compare to a single char (I'm guessing that's where you're heading), you want to use single quotes, like so if(d == '+'). Single quotes for chars, double quotes add an implicit zero-termination on strings. So "+" is actually {'+', 0x0}

于 2012-10-07T23:39:51.103 に答える
0
int main()
{
signed int a, b, c,result;
char d,e;
  cin >> a;
  cin >> b;
  cin >> c;
  cin >> d;
  cin >> e;

if(d=='+')
  if(e=='-')
    result = a + b - c
cout <<result;
}
于 2012-10-07T23:40:32.710 に答える
0
if(d > 0) {

}

if(e < 0) {

}
于 2012-11-25T17:54:43.847 に答える