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 April 29th, 2006, 08:30 PM
haitch haitch is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 1 haitch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 17 m 49 sec
Reputation Power: 0
Problem with cin.get()

Hi

I am new to C++ and have come across a problem I'm having difficulty to solve. The code below is a function which adds data to an array and validates the input. I am having difficulty when a user hit the return key by accident, the code cycles through the options but does not allow any more input.

any help would be great. Thanks

Code:
#define VSIZE 3

void UserInput(char arrStudent [VSIZE][30])
{
system("cls");
cout<<"\n****** Student Detail Screen ******" <<endl<<endl;

	for (int n = 0;n<VSIZE;n++)				
	{
		int nValid = 0;					
		cout << "Details for student number "<<n<<":"<<endl<<endl;
		cout << "Please input Students Name"<<endl;
		cout << "(maximum of 30 characters): ";
		
		do									
		{
		
		cin.ignore();						
		cin.get(chTemp,200,'\n');				
		
			if(strlen(chTemp) =< 0) //this is my atemp at identifiying a carige return
			{
				cout << "Name contains no characters, Please try again: ";
				cin.ignore();
				cin.get(chTemp,200);
			}
			if(strlen(chTemp) > 30)			
			{
				cout << "Name contains " << strlen(chTemp) << " characters, Please input a shorter name: ";
				cin.ignore();
				cin.get(chTemp,200);		
			}
			else 
				{
					nValid = 1;			
			}
		}
		while (nValid = 0);				
		strcpy(arrStudent[n],chTemp);		

	}
}

Reply With Quote
  #2  
Old April 30th, 2006, 05:57 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
Scratch what I just said, I see what your doing now, you could just determine if char == '\n' but you need to process it even though, but one way is to use the array value, or if(char[0] == '\n') then don't continue.

By the way what does system("cls") do and where do you find the rest of the system functions. I know the pause one but couldn't find much about them.
__________________
---Official Member Of The Itsacon Fan Club---
Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer.

Last edited by Geo.Garnett : April 30th, 2006 at 06:14 PM.

Reply With Quote
  #3  
Old April 30th, 2006, 07:07 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
Just some idea's for your program and of course when you put it in the function you can make students amount to the array of students that are already stored so it doesn't overwrite existing. I just wrote this up to give you some idea's and put it in the main function so you could compile it and see what it looks like.
Code:
int main()
{
    int num = 0;
    int students;
    string chTemp;
    string arrStudent[100];
    char ch;
    do
    {
    cout<<"How many students would you like to add : ";
    cin>>students;
    cin.ignore();
    for(int i = 1; i < students + 1; i++)
    {
            cout<<i<<": Students name (30 characters or less)\n";
            
    do
    {
            cout<<"Please enter students name: ";
            num = 0;
            getline(cin,chTemp);
            if(chTemp.length() <= 0)
            {
                 cout<<"Did you forget to add student number: "<<i<<" \n";
                 num = 1;
/*Dont do anything in the two if statemenst because if you dont test the next entered amount its bound to fail*/
                 }
                        if(chTemp.length() > 30)
                        {
                             cout<<"You can only enter 30 characters!\n";
                             num = 1;
                             }
                        }
                        while(num == 1);
                        arrStudent[i]=chTemp;
                        }
            cout<<"==================================================  ==========\n";
            cout<<"You have successfully entered : ";
            for(int l = 1; l <= students; l++)
            {
                    cout<<arrStudent[l]<<", ";
                    }
            cout<<"to your roster.\n";
            cout<<"==================================================  ==========\n";
            cout<<"Would you like to add more (Y/N) : ";
            cin>>ch;
            }
            while(ch != 'Y' || ch != 'y');   
system ("PAUSE");
return 0;
}

Its not much different from yours it just has some few minor changes. It lets the user define how many students to add and then when it completes it verifies if they want to add more or continue.

Last edited by Geo.Garnett : April 30th, 2006 at 07:14 PM.

Reply With Quote
  #4  
Old May 1st, 2006, 03:37 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
Code:
while (nValid = 0);

A common bug. What this says in English is "Keep looping if 0 is successfully assigned to nValid." That is, you are not testing whether nValid is 0; you are assigning 0 to nValid. Therefore, your loop will be infinite.
Comments on this post
Geo.Garnett agrees!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Problem with cin.get()


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


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





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