• C

First character of input string truncated with scanf

Using scanf, with my initial prompt for input , the first character of the input string is truncated.

For example, if I enter George,  only eorge is accepted from the keyboard.  This only occurs on the first scanf.  What am I doing wrong?  See code below.

 /*  Prompt for initial input  */
  printf("\n Enter a student name (First Last) or (0) to Exit : ");
  fflush(stdin); getchar();
  scanf("%14[^\n]s", studentName);


  while (studentName[0] != '0')
  {
    studentCount++;


    fprintf(rpt, "\n %-18s %6.2f %18c", studentName, percentGrade, letterGrade);

    printf("%s", studentName);
    printf("\n Enter a student name (First Last) or (0) to Exit : ");
    fflush(stdin); getchar();
    scanf("%14[^\n]s", studentName);
  }
cookiejarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
It looks like the getchar(); is getting the G character.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sinisa VukSoftware architectCommented:
ozo is right.You want to get Name or 0 for exit with getchar - but first char of (G)eorge was get.
Better:
 do {    
    printf("\n Enter a student name (First Last) or (0) to Exit : ");
    fflush(stdin); 
    scanf("%14[^\n]s", studentName);

    if (studentName[0] != '0')
    {
         studentCount++;
...
         fprintf(rpt, "\n %-18s %6.2f %18c", studentName, percentGrade, letterGrade);
         printf("%s", studentName);
....
    }
  } while (studentName[0] = '0')

Open in new window

sarabandeCommented:
you better use gets for user input instead of scanf.

the user would terminate each input by enter.

char szInput[512] = { '\0' }:
...
if (!gets(szInput)) return -1;
strncpy(studentName, szInput, min(14, strlen(szInput));

...
if (!gets(szInput)) return -1;

Open in new window

studentAge = atoi(szInput);

using gets has the advantage that you don't need care so much for the stdin input stream which behaves strange (especially on windows) with text input. also it is not so easy for the user to spoil the stdin what would prevent from further input until the stdin was not reset.

Sara
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.