Feb 7, 2010 at 8:47am Feb 7, 2010 at 8:47am UTC
I'm trying to write a program that reads input from the user and then performs math on it, using spaces for to seperate the strings such as 1 + 1. I want the user to enter a q or Q to exit the program but I just can't figure out what I'm doing wrong.
do {
cout << "Enter a simple expression. Use spaces for seperation: ";
cin >> firstOperand >> mathSymbol >> secondOperand;
cout << endl;
maxIndex = ( firstOperand.size() + 1);
for ( index = 0; index < maxIndex; index++ ){
if ( firstOperand[index] == '.' ){
stringIsFloat = true;
}
}
if ( stringIsFloat == true ){
firstFloatOperand = atof ( firstOperand.c_str() );
secondFloatOperand = atof ( secondOperand.c_str() );
}
else {
firstIntOperand = atoi ( firstOperand.c_str() );
secondIntOperand = atoi ( secondOperand.c_str() );
}
if ( mathSymbol == '+' ){
if ( stringIsFloat == true){
answerFloat = firstFloatOperand + secondFloatOperand;
}
else {
answerInt = firstIntOperand + secondIntOperand;
}
}
if ( mathSymbol == '-' ){
if ( stringIsFloat == true){
answerFloat = firstFloatOperand - secondFloatOperand;
}
else {
answerInt = firstIntOperand - secondIntOperand;
}
}
if ( mathSymbol == '/' ){
if ( stringIsFloat == true){
answerFloat = firstFloatOperand / secondFloatOperand;
}
else {
answerInt = firstIntOperand / secondIntOperand;
}
}
if ( mathSymbol == '*' ){
if ( stringIsFloat == true){
answerFloat = firstFloatOperand * secondFloatOperand;
}
else {
answerInt = firstIntOperand * secondIntOperand;
}
}
if ( mathSymbol == '%' ){
if ( stringIsFloat == true){
answerFloat = 0;
}
else {
answerInt = firstIntOperand % secondIntOperand;
}
}
if ( stringIsFloat == true ){
cout << firstOperand << " " << mathSymbol << " " << secondOperand
<< " = " << answerFloat << endl;
}
else {
cout << firstOperand << " " << mathSymbol << " " << secondOperand
<< " = " << answerInt << endl;
}
stringIsFloat = false;
}
while ( firstOperand[0] != 'Q' || firstOperand[0] != 'q');
Feb 7, 2010 at 10:43am Feb 7, 2010 at 10:43am UTC
while ( firstOperand[0] != 'Q' || firstOperand[0] != 'q');
try
while (firstOperand[0] != 'Q' && firstOperand[0] != 'q')
{
{Do your code here}
}
hope this helps
Feb 7, 2010 at 10:44am Feb 7, 2010 at 10:44am UTC
sorry didnt realise it was a do-while loop
just change the <|| or> to a <&& and>
Feb 8, 2010 at 1:10am Feb 8, 2010 at 1:10am UTC
Hey,
im not sure what you have your array declared as, but i had the same problem a few days ago with an infinite while loop.
its because i was trying to input a number, i had it declared as a double, and i was using cin
and when i put in a character it spazzed and went nuts lol, and the solution is either to reset the cin input buffer, orr
the way i went about doing it was, declared the input variable as string initially,
then use getline to save the input in the variable, then i converted the string to an integer and did my calculations.
and it works flawlessly.
Hope this helped.
-Jon
Feb 8, 2010 at 5:22am Feb 8, 2010 at 5:22am UTC
I'm afraid a lot of us probably avoided this post for the following reasons:
1) Bad code formatting
2) No ["CODE"] tags used.
3) Long and extended code without pointing out the code in question which also may relate to code formatting and code tags.