where does the error lie? please help me.

This is a discussion on where does the error lie? please help me. within the C++ Programming forums, part of the General Programming Boards category; Hi I have tried my level best to unsuccessfully figure where the error lies. Could you please help me with ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    where does the error lie? please help me.

    Hi

    I have tried my level best to unsuccessfully figure where the error lies. Could you please help me with it? Given below is the code and two outputs which are obviously wrong.

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    
    {
     	system("color 1a");
     	
     	int exp;
     	
     	cout << "enter the expression = ";
     	cin >> exp;
     	
    	cout << "entered expression evaluates = " << exp << endl;
     	
     	system("pause");
     	
    }

    OUTPUT #1:
    Code:
    enter the expression = (5-9)
    entered expression evaluates = 2
    Press any key to continue . . .
    OUTPUT #2:
    Code:
    enter the expression = ((9+18)-18)/9
    entered expression evaluates = 2
    Press any key to continue . . .
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    'exp' is an integer and it can't hold an expression, which is a string.
    And if you entered the expression into a string..it still won't evaluate by itself.
    Remember that the computer is not sentient !
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,856
    The error lies in your expectations. C and C++ I/O do not evaluate expressions like that at all.

    The sequence that will occur when you enter the expression (5-9) is that cin will first contain an opening bracket. That character is examined, deemed not to be a digit of an integral value, and left in the stream. So the streaming operator (invoked by cin >> exp; ) returns immediately, without changing the value of the variable exp.

    The value 2 being output is whatever the value exp happens to hold before you attempt to read input from cin. exp is an uninitialised variable (you have not initialised it with any value) and it stays uninitialised. Accessing the value of an uninitialised variable (and printing a variable means its value must be accessed) formally yields undefined behaviour, according to the C++ standard. That means anything is allowed to happen: a program crash, printing 2, printing 42, or anything else. It just so happens your program retrieves the value 2 and prints that out.

    If you want the user to be able to enter expressions, you have to write code that explicitly interprets the user input, possibly one character at a time, in order to interpret the user input as an expression. I'll leave working out how to do that as an exercise.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks, manasij.

    Quote Originally Posted by grumpy View Post
    The error lies in your expectations. C and C++ I/O do not evaluate expressions like that at all.

    The sequence that will occur when you enter the expression (5-9) is that cin will first contain an opening bracket. That character is examined, deemed not to be a digit of an integral value, and left in the stream. So the streaming operator (invoked by cin >> exp; ) returns immediately, without changing the value of the variable exp.

    The value 2 being output is whatever the value exp happens to hold before you attempt to read input from cin. exp is an uninitialised variable (you have not initialised it with any value) and it stays uninitialised. Accessing the value of an uninitialised variable (and printing a variable means its value must be accessed) formally yields undefined behaviour, according to the C++ standard. That means anything is allowed to happen: a program crash, printing 2, printing 42, or anything else. It just so happens your program retrieves the value 2 and prints that out.

    If you want the user to be able to enter expressions, you have to write code that explicitly interprets the user input, possibly one character at a time, in order to interpret the user input as an expression. I'll leave working out how to do that as an exercise.
    Thank you, Grumpy. Yes, you are right. I simply expected too much from C++.

    Could you please refer to some link where I could find some already made code to manipulate strings contains simple arithmetic operators and numbers? I don't know what to google for and how to insert such a already-made code into my original code in the first post above? Or, perhaps you could help me some other way. Please help me with this.

    Best regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by jackson6612 View Post
    what to google for
    Shunting Yard Algorithm
    Reverse Polish Notation (if you aren't familiar with it )

    But they are (probably) not something good to try when learning the language....
    I implemented those two into a calculator program a few months a go...and parts of were a little....painful..though I enjoyed it a little too much !
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by manasij7479 View Post
    Shunting Yard Algorithm
    Reverse Polish Notation
    (if you aren't familiar with it )

    But they are (probably) not something good to try when learning the language....
    I implemented those two into a calculator program a few months a go...and parts of were a little....painful..though I enjoyed it a little too much !
    Hi manasij

    Thanks for the input. Are you referring to "Shunting Yard Algorithm" and Reverse Polish Notation" as two different algorithms? Wikipedia has this: The shunting-yard algorithm is a method for parsing mathematical expressions specified in infix notation. It can be used to produce output in Reverse Polish notation (RPN) or as an abstract syntax tree (AST).

    So they are not two different algorithms. I used "download Shunting Yard Algorithm for c++" keywords to google the ready-made function but didn't succeed. Please help me with this.

    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Reverse Polish Notation is not an Algorithm. It is , as the name suggests, a notation...for.. expressing expressions (!) .
    Shunting Yard algorithm , as you found out, is used to convert normal(infix) into RPN.
    Downloading a ready made function is a very bad idea ..at least until you understand what is does.
    So ..try something easier first..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21