Hello everyone.
I've just started learning from "Jumping into C++". Great book. Unfortunately, I've also encountered my first snag. Practice problem number 3 from chapter 4 tells me to make a small calculator that takes one of the four arithmetic operations and its two arguments as input and give the result as output. Here's what my newbie mind came up with:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a;
int b;
string operation;
cout<<"a= "<<endl;
cin>>a;
cout<<"b= "<<endl;
cin>>b;
cout<<"What operation would you like performed? (add, subtract, divide, multiply)"<<endl;
getline(cin, operation, '\n');
if(operation=="add")
{
cout<<"a+b="<<a+b;
}
else if(operation=="subtract")
{
cout<<"a-b="<<a-b;
}
else if(operation=="divide")
{
cout<<"a/b="<<a/b;
}
else if(operation=="multiply")
{
cout<<"a*b="<<a*b;
}
cin.ignore();
cin.get();
}
The thing that I don't get is why doesn't the function "getline" work. It's worked in previous programs. The program seems to work if I simply replace getline with a simple "cin". I could easily use that as a cheap fix but I am interested in knowing why "getline" refuses to work anymore.
I hope someone can enlighten me on this.
Thanks.