home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2706
Discussions: 188,384, Posts: 2,243,501, Members: 232,614
Old February 11th, 2003, 02:25 AM   Digg it!   #1 (permalink)
Ultimate Member
 
crouse's Avatar
 
Join Date: Jun 2002
Location: Iowa
Posts: 2,879
Send a message via ICQ to crouse
Question
C++ Question on exiting program from loop

Okay......... keep in mind I'm still learning so don't beat me up to much.

My question is when the program shown below runs..........it works great. UNLESS you type a letter key or something other than a number to exit. If you hit a letter then enter........it goes on forever... what should be included in the code to eliminate the "bug" in the code.......... or a better way to do the loop and exit when needed???
------------------------------
Code:
#include <iostream>

using namespace std;

/* This is to Figure the Area of a Circle,
 Added a loop to this to allow for continued use
until the program encounters another number besides 1 */

float areaofcircle (float x, float y)
{

return ((x*x)*y);
}

int main();
int main()
{

int looping;


     do
     {
     cout << "\n";
     cout << "\n";
     cout << "******************************************** \n";
     cout << "******************************************** \n";
     cout << "\n";
     cout << "\n";
     cout << " AREA OF A CIRCLE \n";
     cout << "\n";
     cout << "\n";
     cout << "To figure the area of a circle we need to \n";
     cout << "have the radius and pi. \n";
     cout << "The formula for the area of a circle is :\n";
     cout << " PI X Radius(2) = AREA \n";
     float a, b, c ;
     cout << "\n";
     cout << "Please enter the Radius of the circle: ";
     cin >> a;
     cout << "PI=3.1415 (for our purposes here): ";
     b=3.1415 ;
     cout << "\n";
     cout << "\n";
     c=areaofcircle(a,b);
     cout << "The AREA of the circle with a radius of " << a << " is " << c << " \n";
     cout << "\n";
     cout << "\n";
     cout << "Would you like to do this again?? \n";
     cout << "Type 1 to continue, or 0 to exit| \n";
     cin >> looping;

     }while ( looping == 1 );

cout << "\n";
cout << "Thanks for using my Area of a Circle program !! \n";
cout << "Good Bye !!\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";


return 0;
}
------------------------------
The code is also posted here : http://www.crouse.ws/viewtopic.php?topic=97&forum=22

Thanks.
__________________
USA Linux Users Group
OpenSUSE.us


Last edited by crouse : February 11th, 2003 at 02:37 AM.
crouse is offline   Reply With Quote
Old February 11th, 2003, 03:29 AM     #2 (permalink)
Ultimate Member
 
crouse's Avatar
 
Join Date: Jun 2002
Location: Iowa
Posts: 2,879
Send a message via ICQ to crouse
Figured it out

Code:
#include <iostream>
using namespace std;

/* This is to Figure the Area of a Circle,
 Added a loop to this to allow for continued use
until the program encounters another number besides 1 */

float areaofcircle (float x, float y)
{return ((x*x)*y);}

int main()
{

int looping;

     do{
          cout << "\n";
          cout << "\n";
          cout << "******************************************** \n";
          cout << "******************************************** \n";
          cout << "\n";
          cout << "\n";
          cout << " AREA OF A CIRCLE \n";
          cout << "\n";
          cout << "\n";
          cout << "To figure the area of a circle we need to \n";
          cout << "have the radius and pi. \n";
          cout << "The formula for the area of a circle is :\n";
          cout << " PI X Radius(2) = AREA \n";
          float a, b, c ;
          cout << "\n";
          cout << "Please enter the Radius of the circle: ";
          cin >> a;
          cout << "PI=3.1415 (for our purposes here): ";
          b=3.1415 ;
          cout << "\n";
          cout << "\n";
          c=areaofcircle(a,b);
          cout << "The AREA of the circle with a radius of " << a << " is " << c << " \n";
          cout << "\n";
          cout << "\n";
          cout << "Would you like to do this again?? \n";
          cout << "Type 1 to continue, or any other key to exit.. \n";
          cin >> looping;

          if ( looping != 1 )
                looping = 2;

     }while ( looping == 1 );

cout << "\n";
cout << "Thanks for using my Area of a Circle program !! \n";
cout << "Good Bye !!\n";
cout << "******************************************** \n";
cout << "******************************************** \n";
cout << "\n";
cout << "\n";

return 0;
}

I just added the line :

Code:
 if ( looping != 1 )
looping = 2;
Now it exits gracefully if a letter or other key is hit instead of the 1 key.

crouse is offline   Reply With Quote
Old February 11th, 2003, 09:36 PM     #3 (permalink)
Member
 
MrFreeze's Avatar
 
Join Date: Oct 2001
Location: Belgium
Posts: 112
you should check the return code of every cin >>x;

http://www.arachnoid.com/cpptutor/student1.html

MrFreeze
__________________
Life Suxss...

MrFreeze is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2896)
CPU wont boot (5)
3-days in and no threads about Gaza (160)
The United States Debt (20)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
hp compaq nc6000 problems (138)
Folderchat Weekday thread (441)
Antec 300 bulk purchase? (11)
Recent Discussions
CPU wont boot (5)
Creative T-3000 Subwoofer (3)
ACPI controller halt on boot (2)
Building a gaming computer advi.. (3)
Worth the upgrade?? (15)
Folderchat Weekday thread (441)
ADVICE (0)
How to increase my ram? (5)
Help with an Ati Radeon HD 4850.. (27)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 10:05 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28