C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 27th, 2004, 05:24 PM
jordban jordban is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 1 jordban User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Cin & endless loop

Hello,
Im writing a little program to better my knowledge of c++ and im having a little bit of difficulty.
Code:
   //_____________________________________________
 // |Program: pie							 |
 // |Date: 12/25/2004						 |
 // |File: main.cpp							 |
   // |Info: Explore different ways of finding pie |
   // |____________________________________________|
   
   #include<iostream>
   
   
   
   using namespace std;
   
   // setting variables
   int input;
   
   main() {
     
     // while user input != 3 loop program
     while(input!=3){
   
   	// prints menu
   	cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
   	cout << "+ How Would you like your pie +" << endl;
   	cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
   	cout << "+ 1) By the diameter		  +" << endl;
   	cout << "+ 2) By the circumference	 +" << endl;
   	cout << "+ 3) I dont want pie		  +" << endl;
   	cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
   	
   	// reset input	 
   	input = 0;
   
   	// gets user input
   	cin >> input;
   	cin.clear();
   	
   	// checks input
   	if (input == 1){  
   	  cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
 	 cout << "+Enter the diameter		 +" << endl;
   	  cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
   	}
   	else if (input == 2) {
   	  cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
   	  cout << "+Enter the circumference	  +" << endl;
   	  cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
   	} 
   	else if (input == 3) {
   	  cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
 	 cout << "+Good Bye! :-)			 +" << endl;
   	  cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
   	}
   	else {
   	  cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
   	  cout << "+We only accept numbers 1-3   +" << endl;
   	  cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" << endl;
   	}
     }
   
   // making os happy
   return 0;
   }
   


For some reason when ever you input something different than a number it just keeps looping(without asking for input) and i have to control c to get out. Anyone know why? \

thanks,
jordan

Reply With Quote
  #2  
Old March 24th, 2005, 09:20 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
yeah, this happens when you input something other than an int when you are trying to get an int using cin.

Reply With Quote
  #3  
Old March 26th, 2005, 12:17 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Quote:
Originally Posted by ubergeek
yeah, this happens when you input something other than an int when you are trying to get an int using cin.



The user is taking another number . He is giving an integer as input.
Hence i do no agree with your explaination.

Reply With Quote
  #4  
Old March 26th, 2005, 10:29 AM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Quote:
Originally Posted by jordban
Hello,
For some reason when ever you input something different than a number it just keeps looping(without asking for input) and i have to control c to get out. Anyone know why? \


The explanation, in my opinion, was excelent. I don't know if true....but accurate!

Reply With Quote
  #5  
Old March 31st, 2005, 02:10 AM
babasaheb babasaheb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 1 babasaheb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 13 sec
Reputation Power: 0
Smile

hey jordan,

just add the following code after cin..

if(!input)
break;

now it works..

baba..

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Cin & endless loop


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not 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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT