PDA

View Full Version : Help: C++ problem on Mac Xcode




f1chen
Dec 3, 2010, 10:56 AM
hi, guys

when I run the following code in Xcode3.2.1 on Mac Snow Leopard 10.6.5,
the code dose't work as I suppose.

#include<iostream>
using namespace std;

int main()
{
cout<<"enter expression:";
int lval,rval;
char op;

cin>>lval;
if (!cin) cout<<"no first operand";

while (cin>>op) {
cin>>rval;
if (!cin) cout<<"no second operand";
switch (op) {
case '+':
lval+=rval;
break;
case '-':
lval-=rval;
break;
case '*':
lval*=rval;
break;
case '/':
lval/=rval;
break;
default:
cout<<"result:"<<lval<<"\n";
break;
}
}
cout <<"bad expression";

return 0;
}

if I enter: 10+10 return
no results at all
so I enter a random integer and return again
no results still
so I enter another random integer and return again
it displays: result:20

however the program doesn't end, it seems like waiting for input.

I tried ctrl+D or ctrl+U, they don't work either.
:confused:



chown33
Dec 3, 2010, 11:14 AM
Explain how you expect it to work.

Also describe how you'd break the problem down into smaller testable programs, so you can test cin>>lval, cin>>op, etc. as individual pieces.

mac2x
Dec 3, 2010, 01:38 PM
EOF does not work because you aren't testing for that condition*. chown33's advice to tell us what you expect, etc. is a good idea; it will make sure you understand what is going on.

hint: use the eof() function. ;)

chown33
Dec 3, 2010, 02:06 PM
You also have a significant bug that's causing the lack of output. Breaking it down into smaller individually testable pieces, and then building it back up piece by piece, testing at each stage, should show where that problem lies. I could point you to it, but then you wouldn't learn much about how to debug, test, decompose problems, and recompose working parts into composite wholes.

mac2x
Dec 3, 2010, 04:45 PM
You also have a significant bug that's causing the lack of output. Breaking it down into smaller individually testable pieces, and then building it back up piece by piece, testing at each stage, should show where that problem lies. I could point you to it, but then you wouldn't learn much about how to debug, test, decompose problems, and recompose working parts into composite wholes.

@the OP: I basically did the same thing on your program to make a bug free and far more robust version. Very sound advice.

Also, it's a very good idea to write an algorithm BEFORE you write any code.

Figure out exactly what you want to do and what order it needs to be done in.

Perhaps write some pseudocode and "run" that according to how a C++ program runs, using a pencil, paper, and calculator if needed. Naturally, this means you need to have some idea about how things (e.g. cin and cout) work already.

If you can develop a working algorithm, you've done all the hard work. Just translate your pseudocode into C++. If all is well the only problems you will find will likely be syntax errors.

I will be glad to share my version once you have squashed the bugs in yours and it works as expected. :)

f1chen
Dec 4, 2010, 11:47 AM
thanks for all of you, I really appreciate your precious recommendations/suggestions:)

what I want to implement is:
input an expression in pattern like this: number1 op number2 op number3...
here the op could be one of the four computation operations (+ / * -)

for example:
input: 1+2 expected output: result:3
input: 1+2+3 expected output: result:6


I can handle the 1+2 case in fact
#include<iostream>
using namespace std;

int main()
{
cout<<"enter expression:";
int lval,rval;
char op;

cin>>lval;
if (!cin) cout<<"no first operand";


cin>>op;
cin>>rval;
if (!cin) cout<<"no second operand";

switch (op) {
case '+':
lval+=rval;
break;
case '-':
lval-=rval;
break;
case '*':
lval*=rval;
break;
case '/':
lval/=rval;
break;
default:
break;
}

if(op) cout<<"result:"<<lval<<"\n";

return 0;
}

however, if I wanna process the case where the number of op is more than one, and add the while in the code(just as in the first post), it just dosen't work as I predict.

Under Xcode, I input 1+2 under debug mode, and find out that the operands and the numbers were correctly assigned to the variables, things goes well in the switch block, but when again come to check the
while (cin>>op)
it broke down.

I guess, maybe that's for the RETURN button?

Could you just give me more clues or hints, mac2x?

chown33
Dec 4, 2010, 12:46 PM
You're approaching the problem from the wrong direction. You think because you've solved the "input a number and op" part that you can just add on the additional op and number in a loop. But since that's where you have the problem, you need to start over and address just the loop.

Write a program that loops, and just inputs a number, until the input number is 0. The program should perform a constant calculation on the input number and display the result. For example, multiply the input by 3 and display that.

After you have that program working, you can change the terminating condition. Instead of inputing a number 0 to terminate the loop, have it detect EOF (control-D) on the input.

Next, repeat the above where you input a number and an operation until EOF. It should output the number and the operation, but do nothing else (except terminate correctly). It should then be modified to ignore whitespace, and to reject and recover from invalid inputs (such as a number entered when an op should be).

You need to learn how these pieces work in isolation. How looping works, how input-EOF detection works, how different types of input work (number vs. operation). After you understand how each one works by itself, then you can combine them in more complex ways. But if you don't know how it works by itself, then it will be almost impossible to learn it by adding it to an already complex program.

If there's some isolated part you can't figure out, such as detecting EOF on input, then you should refer to the reference manual or textbook that describes that part, or find an example that shows that part working. For example, you could google c++ cin eof or c++ cin while

mac2x
Dec 4, 2010, 01:06 PM
Chown gives good advice (ask me how I know!). :) I don't have anything to add other than follow it.

lloyddean
Dec 4, 2010, 02:19 PM
A nice C++ library reference here (http://www.cppreference.com/wiki/).

f1chen
Dec 5, 2010, 06:37 AM
PROBLEM SOLVED:)

In Xcode, to end a stream, input Control-Q and Control-D in sequence.

it works!

mac2x
Dec 5, 2010, 03:23 PM
What exactly do you mean?

f1chen
Dec 5, 2010, 08:28 PM
What exactly do you mean?

input CTRL+D in Xcode equals to enter CTRL+Z in Windows:)

mac2x
Dec 6, 2010, 07:49 PM
Indeed. But did you learn how to 1) test for EOF and 2) deal with error conditions on the stream?

f1chen
Dec 7, 2010, 02:45 AM
Indeed. But did you learn how to 1) test for EOF and 2) deal with error conditions on the stream?

cin.eof()

however, I'm still in the procedure of mastering error handling~