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 June 4th, 2006, 11:05 PM
MScoops MScoops is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Posts: 5 MScoops User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 39 sec
Reputation Power: 0
Help with loop

Code:
#include <iostream.h>
#include "G:\Adv Prog\C++ files\lvp\String.h"

void main()
{
	int again=0;
	do
	{
		int num=0;
		cout<<"Enter num: ";
		cin>>num;
		if(num==1)
		{
			cout<<"one";
			again=0;
		}
		else if(num==2)
		{
			cout<<"two";
			again=0;
		}
		else
		{
			num=0;
			again=1;
		}
	}while(again==1);
	cout<<"\nDONE\n";
}


I'm trying to figure out how to polish my code up so that if you enter a 'g' it will loop to the top and allow you to re-enter a number. If you enter any integer other than 1 or 2 it will work perfectly and loop to the top. If you enter anything other than an integer it will keep looping and won't let you input another integer. How do I get it so that it loops to the top when anything other than an integer is entered??
Thanks~

Reply With Quote
  #2  
Old June 5th, 2006, 12:45 AM
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
a couple things generally:
-what is the String.h for? not needed in this program
-<*.h> is the old deprecated format for standard headers, use <*> instead and put using namespace std; after the #include line
-main must return int, and you should put return 0 at the end

Now on to your question. You ask cin to expect an int, so when it gets a character it goes into the fail state. Then the next input does nothing, so you go into an endless loop. To fix this, add this code before the cin>>num line:
Code:
if (!cin) //test for failure state
{
     cin.clear(); //clear failure state
     char c = '\0';
     cin >> c; //get the character that was input
     if (c == 'g') num = -1;
}
//then add another if statement after the input
if (num == -1)
{
    cout << "gee";
    again = 0;
}

This makes it so any character besides g will also cause termination, while g will trigger continuation.

Reply With Quote
  #3  
Old June 5th, 2006, 01:53 PM
MScoops MScoops is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Posts: 5 MScoops User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 39 sec
Reputation Power: 0
Quote:
Originally Posted by ubergeek
a couple things generally:
-what is the String.h for? not needed in this program
-<*.h> is the old deprecated format for standard headers, use <*> instead and put using namespace std; after the #include line
-main must return int, and you should put return 0 at the end

Now on to your question. You ask cin to expect an int, so when it gets a character it goes into the fail state. Then the next input does nothing, so you go into an endless loop. To fix this, add this code before the cin>>num line:
Code:
if (!cin) //test for failure state
{
     cin.clear(); //clear failure state
     char c = '\0';
     cin >> c; //get the character that was input
     if (c == 'g') num = -1;
}
//then add another if statement after the input
if (num == -1)
{
    cout << "gee";
    again = 0;
}

This makes it so any character besides g will also cause termination, while g will trigger continuation.



Thanks bro ^^

Reply With Quote
  #4  
Old June 5th, 2006, 11:22 PM
xreddawg909x xreddawg909x is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 91 xreddawg909x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 27 m 20 sec
Reputation Power: 4
Code:
int main()
{
int again =0;
int num=0;
    do{
    
      cout<<"Enter number: ";
      cin>>num;
       switch(num)
       {
          case 1 : 
            cout<<"one<<endl;
            again  = 0;
            break;
          case 2: 
            cout<<"two"<<endl;
           again = 0;
            break;
          default:
           num=0
          again = 1;
           
      }

}while(again ==1);
cout<<"done"<<endl;
return 0;
}


Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with 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-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT