Thread: Jumping into C++ : Chapter 6 Practice Problem 2

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    Jumping into C++ : Chapter 6 Practice Problem 2

    Hello everyone, i have started to learn C++ with the help of the book called Jumping into C++. Now i have hit the first wall, i am not sure what i am doing wrong, or i just lack knowledge to make it work.

    My task says:

    Make your calculator program perform computations in a seperate function for each type of computation.

    My only problem is, when i am about to add a value for x or y and i add NOT a number, so for example a letter like f. Then my while loop becomes an infinite loop with the infinite spam of else statement.

    I am stuck at this for 2 days now, and i do not really wanna move forward more in the book until i do not finish all the Practice Problems.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int add(int x, int y)
    {
        return x+y;
    }
    
    int subtract(int x, int y)
    {
        return x-y;
    }
    
    int divide(int x, int y)
    {
        return x/y;
    }
    
    int multiply(int x, int y)
    {
        return x*y;
    }
    
    int main()
    {
        int x;
        int y;
        string symbol;
        cout<< "Enter your first number: ";
        cin>>x;
        cout<< "Enter your second number: ";
        cin>>y;
        cout<< "Enter your argument: ";
        cin>>symbol;
        
        while(1)
        {
            if(symbol=="+")
            {
                cout<< "Your number is: ";
                cout<< add(x,y);
                break;
            }
            else if(symbol=="-")
            {
                cout<< "Your number is: ";
                cout<< subtract(x,y);
                break;
            }
            else if(symbol=="/")
            {
                if(y==0)
                {
                    cout<< "Divide cannot be done with 0, please try again.\n";
                    cout<< "Enter your second number again: ";
                    cin>>y;
                }
                cout<< "Your number is: ";
                cout<< divide(x,y);
                break;
            }
            else if(symbol=="*")
            {
                cout<< "Your number is: ";
                cout<< multiply(x,y);
                break;
            }
            else
            {
                cout<< "Invalid character(s). Please try again.\n";
                cout<< "Enter your first number: ";
                cin>>x;
                cout<< "Enter your second number: ";
                cin>>y;
                cout<< "Enter your argument: ";
                cin>>symbol;
            }
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Stream extraction is type aware, so if you try and read 'f' into an integer, it will just fail.

    Code:
    if ( cin>>x ) {
      // success
    } else {
      // failed, do something to clean up
    }
    It is common to use cin.ignore() to get rid of erroneous input. Numerous examples can be found on how to do this (like our FAQ).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Markkusz
    My only problem is, when i am about to add a value for x or y and i add NOT a number, so for example a letter like f. Then my while loop becomes an infinite loop with the infinite spam of else statement.
    This is due to the invalid input remaining in the input stream. When you try to read from the input stream, you end up reading the same invalid input, over and over again.

    There are two general approaches to fixing this:
    • Discard the invalid characters, e.g., by calling cin.ignore(), then call cin.clear() to remove any error flags associated with the input stream, and then read the next input.
    • Read a line with std::getline() into a std::string object, then use the std::string object to initialise a std::stringstream, then extract the value from the string stream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    2
    Thanks for the answers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping into c++ practice problem poker chapter 9
    By menkle in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2015, 02:43 PM
  2. Question on "Jumping into C++" Chapter 13 practice problem 1
    By Ben Sturm in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2015, 01:16 PM
  3. Linked Lists Chapter 15 Practice Problem 1 Jumping Into C++
    By ArtemisFowl2nd in forum C++ Programming
    Replies: 34
    Last Post: 04-30-2014, 12:59 PM
  4. Jumping Into C++ Chapter 14 Practice Problem 1
    By ArtemisFowl2nd in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2014, 09:36 PM
  5. Replies: 2
    Last Post: 02-24-2014, 05:51 PM