Thread: C++ programing , Loops , infinite loops issue

  1. #1
    Registered User Ibrahim Lawleit's Avatar
    Join Date
    Jul 2014
    Posts
    8

    Unhappy C++ programing , Loops , infinite loops issue

    Hi,
    I am new to this forum, so please don't be mean to me if I didn't know what to say or what to write or not.

    My Problem starts with the idea of creating a calculator
    and I tried to do that using everything I learn so far (I'm still a beginner)
    and I ended up with this code that worked perfectly in the beginning until I decided to test it and I found this infinite loops issue
    this is the code
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    double add(double x,double y);
    double subtract(double x,double y);
    double multiply(double x,double y);
    double divide(double x,double y);
    
    
    int main()
    {
        int i = 0;
        while( i == 0 )
        {
           double x, y;
           char op;
    
    
           cin >> x;  //the first argument to the numerical operation
           cin >> op; //The numerical operation indicator
           cin >> y;  //The second argument to the numerical operation
    
    
           /*
           we are going to use four condition because we know four numerical operations +,-,*,/.
           */
    
           if(op == '+')
            {
                cout<< "= " << add(x,y) << endl;
                continue;
            }
          else if(op == '-')
            {
                cout<< "= " << subtract(x,y) << endl;
                continue;
            }
           else if(op == '*')
            {
                cout << "= " << multiply(x,y) << endl;
                continue;
            }
           else if(op == '/')
            {
                cout << "= " << divide(x,y) << endl;
                continue;
            }
            else
            {
                ++i;
            }
        }
    }
    
    
    
    
    double add(double x,double y)
    {
        return x + y;
    }
    
    
    double subtract(double x,double y)
    {
        return x - y;
    }
    
    
    double multiply(double x,double y)
    {
        return x * y;
    }
    
    
    double divide(double x,double y)
    {
        return x / y;
    }
    the problem is
    when I enter a character from the start other than +,-,*,/ the programs stops and that what i want
    But when i do this
    Attachment 13583
    Attachment 13584
    Attachment 13585

    Please i need your help with this , i tried all the fixes i could think of but none of them solved the problem
    Last edited by Ibrahim Lawleit; 07-14-2014 at 04:03 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly is this "infinite loop issue"?
    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

  3. #3
    Registered User Ibrahim Lawleit's Avatar
    Join Date
    Jul 2014
    Posts
    8
    sorry i just edited the post , i am still new to this forums.
    please feel free to copy the code and test it

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you need the variable named i?

    As for the issue: you should check that the input was successful, e.g.,
    Code:
    if (cin >> x >> op >> y)
    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

  5. #5
    Registered User Ibrahim Lawleit's Avatar
    Join Date
    Jul 2014
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Why do you need the variable named i?

    As for the issue: you should check that the input was successful, e.g.,
    Code:
    if (cin >> x >> op >> y)
    i used the i variable so that if the variable op didn't meet any of the conditions it will result in ++i which will force the condition to change and become false because i is not equal to 0 any more

    and can you please explain more the solution you gave Me ; i am still new to c++ :/

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ibrahim Lawleit
    i used the i variable so that if the variable op didn't meet any of the conditions it will result in ++i which will force the condition to change and become false because i is not equal to 0 any more
    Use a bool variable instead.
    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

  7. #7
    Registered User Ibrahim Lawleit's Avatar
    Join Date
    Jul 2014
    Posts
    8
    Quote Originally Posted by laserlight View Post
    Use a bool variable instead.
    thanks for the help ^_^ , that was really helpful

  8. #8
    Registered User Ibrahim Lawleit's Avatar
    Join Date
    Jul 2014
    Posts
    8
    nope the problem still there i thought that was enough to fix it

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    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

  10. #10
    Registered User Ibrahim Lawleit's Avatar
    Join Date
    Jul 2014
    Posts
    8
    Code:
    #include <iostream>
    
    
    
    
    using namespace std;
    
    
    double add(double x,double y);
    double subtract(double x,double y);
    double multiply(double x,double y);
    double divide(double x,double y);
    
    
    int main()
    {
        
        while(true)
        {
           double x, y;
           char op;
    
    
           cin >> x;  //the first argument to the numerical operation
           cin >> op; //The numerical operation indicator
           cin >> y;  //The second argument to the numerical operation
    
    
           /*
           we are going to use four condition because we know four numerical operations +,-,*,/.
           */
    
    
    
    
           if(op == '+')
            {
                cout<< "= " << add(x,y) << endl;
                continue;
            }
          else if(op == '-')
            {
                cout<< "= " << subtract(x,y) << endl;
                continue;
            }
           else if(op == '*')
            {
                cout << "= " << multiply(x,y) << endl;
                continue;
            }
           else if(op == '/')
            {
                cout << "= " << divide(x,y) << endl;
                continue;
            }
            if(!( cin>>x>>op>>y ) )
            {
                break;
            }
        }
    }
    
    
    
    
    double add(double x,double y)
    {
        return x + y;
    }
    
    
    double subtract(double x,double y)
    {
        return x - y;
    }
    
    
    double multiply(double x,double y)
    {
        return x * y;
    }
    
    
    double divide(double x,double y)
    {
        return x / y;
    }

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>if(!( cin>>x>>op>>y ) )
    This statement reads into x, op and y. So you are essentially doing it twice.
    The idea is that when you first read into x, op and y, you need to check if it worked or not and then take appropriate measures.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It might also help if you gave a sample of the input you're supplying that causes your code to demonstrate the concern.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Registered User Ibrahim Lawleit's Avatar
    Join Date
    Jul 2014
    Posts
    8
    if you saw the first post i included some pictures that demonstrate the issue , and i also said that i am still a beginner or a Nooooob , Total Noob
    if You could please explain yourselves, i would really appreciate it .

    what does this code mean if(cin >> x >> op >> y) ?
    and how can i use it to fix my problem?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>cin >> x >> op >> y
    This means first read a double into x, then read one character, then read one double into y.
    Each operation (cin >> var) returns the stream itself, allowing you to chain operations.

    So the result of (cin >> x) is std::cin itself.
    Now, that means that (cin >> x >> op >> y) return std::cin itself.
    If for some the operator >> fails (i.e. fail to read some input), then the input stream goes into an error state. You can check if the stream if still in a good condition by using an if statement, like if (std::cin).
    If this checks fails, it means a read failed, so you can do an appropriate action.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User Ibrahim Lawleit's Avatar
    Join Date
    Jul 2014
    Posts
    8
    Quote Originally Posted by Elysia View Post
    >>cin >> x >> op >> y
    This means first read a double into x, then read one character, then read one double into y.
    Each operation (cin >> var) returns the stream itself, allowing you to chain operations.

    So the result of (cin >> x) is std::cin itself.
    Now, that means that (cin >> x >> op >> y) return std::cin itself.
    If for some the operator >> fails (i.e. fail to read some input), then the input stream goes into an error state. You can check if the stream if still in a good condition by using an if statement, like if (std::cin).
    If this checks fails, it means a read failed, so you can do an appropriate action.
    i fixed the problem using if(cin >> x >> op >> y)
    can you please private message Me , I really Need you help , if you could please explain somethings to Me.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I keep getting infinite loops?
    By vxs8122 in forum C Programming
    Replies: 6
    Last Post: 05-30-2014, 05:18 PM
  2. Help with infinite loops
    By asanchez1171 in forum C Programming
    Replies: 1
    Last Post: 10-14-2011, 11:23 AM
  3. Infinite Loops in C
    By DaniiChris in forum C Programming
    Replies: 8
    Last Post: 07-06-2008, 12:21 PM
  4. please help, infinite loops!
    By HybridM in forum C++ Programming
    Replies: 18
    Last Post: 01-14-2003, 09:27 PM
  5. i have infinite loops. why?
    By hermit in forum C Programming
    Replies: 20
    Last Post: 04-17-2002, 06:58 AM

Tags for this Thread