dimanche 5 août 2018

Not acknowledging else if statement in c++?

I am a total beginner in programming but am learning c++. I wanted to make a simple calculator but when I run my code in code::blocks, it reads my else if statements as simply"else"; and gives 'else' without a previous 'if' error. It works when I make all the statements as if, but then all the answers are displayed on the screen. Help please!

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
  int a,b;
  std::string op;

  cout<<"enter a number please"<<endl;
  cin>> a;

  cout<<"enter another number please"<<endl;
  cin>> b;

  int x=a+b;
  int y=a*b;
  int z=a-b;
  int t=a/b;

  cout<<"x->addition"<<endl;
  cout<<"y->multiplication"<<endl;
  cout<<"z->substraction"<<endl;
  cout<<"t->division"<<endl;

  cout<<"which operator would you like to use?"<<endl;

  cin>>op;

  if(op=="x");
  {
    cout<<"answer is:"<<x<<endl;
  }
  else if(op=="y");
  {
   cout<<"answer is:"<<y<<endl;
  }
  else if(op=="z");
  {
    cout<<"answer is:"<<z<<endl;
  }
  else if(op=="t");
  {
    cout<<"anser is:"<<t<<endl;
  }

  return 0;
  }

Aucun commentaire:

Enregistrer un commentaire