Help: C++ problem on Mac Xcode

Discussion in 'Mac Programming' started by f1chen, Dec 3, 2010.

  1. macrumors newbie

    Joined:
    Oct 21, 2010
    #1
    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.

    Code:
    #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:
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. macrumors 65816

    Joined:
    Sep 19, 2009
    #3
    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. ;)
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    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.
     
  5. macrumors 65816

    Joined:
    Sep 19, 2009
    #5
    @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. :)
     
  6. f1chen, Dec 4, 2010
    Last edited: Dec 4, 2010

    thread starter macrumors newbie

    Joined:
    Oct 21, 2010
    #6
    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
    Code:
    #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
    Code:
    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?
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
    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
     
  8. macrumors 65816

    Joined:
    Sep 19, 2009
    #8
    Chown gives good advice (ask me how I know!). :) I don't have anything to add other than follow it.
     
  9. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  10. thread starter macrumors newbie

    Joined:
    Oct 21, 2010
    #10
    PROBLEM SOLVED:)

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

    it works!
     
  11. macrumors 65816

    Joined:
    Sep 19, 2009
  12. thread starter macrumors newbie

    Joined:
    Oct 21, 2010
    #12
    input CTRL+D in Xcode equals to enter CTRL+Z in Windows:)
     
  13. macrumors 65816

    Joined:
    Sep 19, 2009
    #13
    Indeed. But did you learn how to 1) test for EOF and 2) deal with error conditions on the stream?
     
  14. thread starter macrumors newbie

    Joined:
    Oct 21, 2010
    #14
    cin.eof()

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

Share This Page