
need pretty simple inputting help
I need a bit of help with inputting a string that is only
10 chars long, including the null. I am writing a program
for a teacher to enter a student name, ID#, and grade.
The student name needs to be only 10 chars long (with the
null), but if a 15 char name is entered, the name must be
truncated. here is the code I am using. It works very
well, except when I enter a name that has a space in the
middle of it. i.e. Van Mallo. With my code the Van is
entered for student, but Mallo goes into the ID array. I
would appreciate some help if it's at all possible.
Thank You
Jerry
void input(void)
{
char input_string[200];
for (int count = 0; count < 5; count++)
{
cout << "\nPlease enter student " << (count +1)
<< " name, ID, and grade.\n";
cout << "Press enter after each entry.\n ";
// cin.width(10);
cin >> input_string;
strncpy( student[count], input_string, 10 );
student[count][9] = NULL;
cin >> input_string;
strncpy( id[count], input_string, 7 );
id[count][6] = NULL;
do
{
cin >> grade[ count ];
acgrade += grade[ count];
if( grade[ count ] > 100 || grade[
count] <0)
cout << "Please enter a value between 0 and 100!";
} while (grade[ count ] > 100 || grade[
count] < 0);
do
{
cout << "Please enter enter Y if the
student is degree seeking, N if not.\n ";
cin >> input_string;
deg[ count ] = input_string[0];
if (deg[count] == 'n')
deg[count] = 'N';
if( deg[count] == 'y')
deg[count] = 'Y';
} while ((deg[count] != 'Y') && (deg
[count] != 'N'));
got_data = true;
}
Quote:
}