Hi!
I'm a Computer Science student doing my first year and I'm still pretty new to C++, we havent't really started with it yet.
But anyway, I'm trying to write a simple calculator that doesn't have any operator precedence. So 4 + 3 * 5 = 35 and 4 * 3 + 5 = 17.
int main(){
int number;
float total;
char sign;
while(sign != '='){
cin >> number;
cin >> sign;
if(sign == '+'){
total += number;
}
else if(sign == '-'){
total -= number;
}
else if(sign == '*'){
total *= number;
}
else if(sign == '/'){
total /= number;
}
}
cout << "Answer = " << total << endl;
return 0;
}
int main(){
int number;
float total;
char sign;
while(sign != '='){
cin >> number;
cin >> sign;
if(sign == '+'){
total += number;
}
else if(sign == '-'){
total -= number;
}
else if(sign == '*'){
total *= number;
}
else if(sign == '/'){
total /= number;
}
}
cout << "Answer = " << total << endl;
return 0;
}
To copy to clipboard, switch view to plain text mode
I'm just doing it all in Main() and I'm using a while loop but this isn't working.
Does anyone have a better idea?
appreciate any advise!
Cheers!
Bookmarks