need pretty simple inputting help 
Author Message
 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:
}



Sat, 16 Oct 2004 22:14:50 GMT  
 need pretty simple inputting help
inline..


Quote:
> 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;

cin >> reads till it gets a white space charecter. Use getLine() instead.

- Show quoted text -

Quote:
>       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;
>    }
> }



Sun, 17 Oct 2004 20:41:10 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Help about simple painless input needed.

2. Help: Need Pretty Printer for C

3. Help - Simple input/output

4. Help - Simple input/output

5. Pretty simple:

6. need help on direct keyboard input

7. Need help with input data from file

8. Need help inputting Float #'s

9. Need help w/unbuff'd input.

10. Need help with input from text file

11. HELP:Need hidden password input

12. Need help/input/etc.

 

 
Powered by phpBB® Forum Software