| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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);
}
}
|
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Problem with cin.get() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|