Xcode Infinite Loop

Discussion in 'Mac Programming' started by My iPhone3G, Aug 12, 2015.

  1. My iPhone3G, Aug 12, 2015
    Last edited by a moderator: Aug 12, 2015

    My iPhone3G macrumors newbie

    Joined:
    May 2, 2012
    #1
    So heres my code. It seems to me that cin.good doesn't work in Xcode. Any help would be wonderful.
    Code:
    #include <iostream>
    
    #include <ctype.h>
    
    usingnamespace std;
    
    void Pyramid(int base, int height){
    
       float pyramid;
    
        pyramid=(1.0/3)*base*height;
    
        cout<<"The volume is "<<pyramid<<"\n";
    
    }
    
    
    
    int main() {
    
       int base, height;
    
       for (int x=0; x==0[IMG]http://linustechtips.com/main/public/style_emoticons/default/wink.png[/IMG] {
    
            cout << "Enter the base then the height.\n";
    
            cin>>base;
    
           if (!cin.good()) {
    
                cin.clear();
    
                cin.sync();
    
                cout<<"Please enter numbers higher than 0\n";
    
                cin>>base;
    
            }
    
            cin>>height;
    
           if (!cin.good()) {
    
                cin.clear();
    
                cin.sync();
    
                cout<<"Please enter numbers higher than 0\n";
    
                cin>>height;
    
            }
    
           if (base>0 && height>0)
    
            {
    
                Pyramid(base,height);
    
                x=1;
    
            }
    
           if (base<=0||height<=0) {
    
                cout<<"Please enter numbers higher than 0\n";
    
                cin>>base>>height;
    
            }
    
        }
    
       return 0;
    
    }
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Describe exactly what you did. Post your exact input.
    Describe exactly what happened as a result. Post your output.
    Describe what you expected to happen.


    When I run the code you posted, it works for me. I don't see an infinite loop. However, it does seem like it asks for base and height and then may ask for them again without using previously entered values. That's a logic error (your programming), not a failure of Xcode.

    It also doesn't behave properly if I press control-D (the keystroke for EOF) when it asks for input. Here's the output:
    Code:
    Enter the base then the height.
    Please enter numbers higher than 0
    Please enter numbers higher than 0
    The volume is 2.01934e+13
    
    I pressed control-D immediately after the prompt for "Enter the base then the height.". The output was produced exactly as shown (copy and pasted from Terminal window), and then the program exited and the shell re-prompted. So again, no infinite loop, but not a correct result for volume.
     
  3. My iPhone3G thread starter macrumors newbie

    Joined:
    May 2, 2012
    #3
    I feel dumb. It's an infinite loop when I put in something that is not an integer.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Describe exactly what you did. Post your exact input.
    Describe exactly what happened as a result. Post your output.
    Describe what you expected to happen.

    Details are important. If you don't provide details, no one can help you, because we don't know exactly what you did.


    Here's what I did.

    I compiled the program in Terminal, and ran it from a Terminal window. I typed in "cod fish" when prompted for base and height. Neither of those is an integer.

    Here's what happened (complete transcript of output and input):
    Code:
    Enter the base then the height.
    cod fish
    Please enter numbers higher than 0
    Please enter numbers higher than 0
    The volume is 1.87111e+13
    After printing the erroneous volume, the program exited back to the Terminal window shell. There was no infinite loop.
     

Share This Page