Oct 7, 2016 at 11:45am UTC
so im having trouble inputting a number with commas and then reading the whole value.. the program will believe that the comma is an operator and therefore doesnt run the way i want to do. it just leaves it blank or some weird number..
#include <iostream>
#include <cstdlib>
#include <sstream>
#include "fraction.h"
#include <vector>
using namespace std;
void perform(double first, char op, double second, fraction &answer);
bool getInput(string &line);
int main()
{
string line;
double first,second;
fraction answer;
stringstream ss;
char op;
while(getInput(line))
{
cout <<"You entered: "<<line<<endl;
ss<<line;
ss>>first;
ss.ignore();
ss>>op;
ss>>second;
ss.ignore();
answer.setValue(first,second);
cout<<"First = "<<first<<" op = "<<op<<" Second = "<<second<<endl;
perform(first,op,second,answer);
cout<<first<<" "<<op<<" "<<second<<" = "<<answer;
}
return 0;
}
bool getInput(string &line)
{
cout<<"Expression: \n";
getline(cin,line);
return !line.empty();
}
void perform(double first, char op, double second, fraction& answer)
{
cout<<"Starting perform and\nFirst = "<<first<<" op = "<<op<<" Second = "<<second<<endl;
switch(op)
{
case '+' : answer = first + second;
break;
case '-' : answer = first - second;
break;
case '*' : answer = first * second;
break;
case '/' : answer = first / second;
break;
default : answer = 0;
}
cout<<"Ending perform and answer = "<<answer<<endl;
exit(0);
}
Oct 7, 2016 at 12:00pm UTC
Read it in as a string and convert to a int. You have to do the same thing if you read in $123.50