Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

My iPhone3G

macrumors newbie
Original poster
May 2, 2012
19
14
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;

}
 
Last edited by a moderator:
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.
 
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.
I feel dumb. It's an infinite loop when I put in something that is not an integer.
 
I feel dumb. It's an infinite loop when I put in something that is not an integer.
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.