newbie needs inputting string help 
Author Message
 newbie needs inputting string 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:17:35 GMT  
 newbie needs inputting string help

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

Jerry, I looked at your function a little and noticed that
you have written "Press enter after every entry"

This doesn't actually do anything as the program stands.
The reason the second name is lost is that it ends up
being read into the id.  

Hope this is of some use.  Good luck, Mark



Mon, 18 Oct 2004 02:03:44 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. newbie needs help to strings, strings, strings

2. newbie needs string help

3. eval(string)?, or, 'nother newbie needs help

4. newbie: input string into pointers

5. Input String Question (Newbie)

6. Newbie question: multi-char string input

7. Standard method for getting string input ?(newbie question)

8. Newbie help with keyboard input

9. Newbie help with keyboard input

10. allocating unlimited string input....HELP

11. help string input

12. limiting string input help

 

 
Powered by phpBB® Forum Software