2

I am reading from stdin students in a structure array. After the details are introduced for one student, i ask for another student details. if the choice is Y, i'll add the new student, if the choice is N, break. But what if the choice is simply ENTER? How can i detect the new line character? I tried with getchar(), but it skips the first reading from stdin.When i debug it doesn't stops to the first line test=getchar(), it stops to the second one.

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>

struct student
{
char name[20];
int age;
};

int main()
{
struct student NewStud[5];
char test;
int count=0;
for(count=0;count<5;count++)
{
    printf("Enter the details for %s student: ",count>0?"another":"a");
    printf("\nName : ");
    scanf("%s",NewStud[count].name);
    printf("\nAge : ");
    scanf("%d",&NewStud[count].age);
    printf("Would you like to continue? (Y/N)");
    test=getchar();
    if(test=='\n')
    {
        printf("Invalid input. Would you like to continue? (Y/N)");
        test=getchar();
    }
    while(tolower(test) !='n' && tolower(test) != 'y')
    {
        printf("Invalid input.Would you like to continue? (Y/N)");
        test=getchar();
    }
    if(tolower(test) == 'n')
    {
        break;
    }
    if(tolower(test) == 'y')
    {
        continue;
    }
}


getch();
}
laura
  • 2,085
  • 13
  • 36
  • 68
  • 1
    This doesn't look like C++. In the future please tag either C or C++ as both languages can have a different style of accomplishing your task. – Marlon Sep 17 '12 at 18:22
  • It's really hard to understand what your issues is. What exactly does "it skips the first reading from stdin" mean? (99% chance your issue is that someone types "Y enter" and you read only one character, leaving the enter to read the next time you call `getchar`. Use a function that reads lines.) – David Schwartz Sep 17 '12 at 18:27
  • This looks like a homework question. Homework questions get a homework tag. I'm pretty sure it is, so I'll go ahead and add it for you. If you disagree, you can remove it yourself. Edit: uhh, obselete and in the process of being removed? *goes to meta* – Wug Sep 17 '12 at 18:27
  • 2
    @DavidSchwartz - Laura can correct me if I'm wrong, but I'm pretty sure the problem is that the prompts "...(Y/N)" is displayed and then it goes right away to the "Invalid input" message. Due to the '\n' being left on the input buffer because of the use of `scanf()` – Mike Sep 17 '12 at 19:18

4 Answers4

2

The problem is that scanf() leaves a newline character in the input stream, you have to consume it before you'll get "valid" data in getchar().

Ex:

scanf("\n%s",NewStud[count].name);
getchar();
printf("\nAge : ");     
scanf("%d",&NewStud[count].age);
getchar();
printf("Would you like to continue? (Y/N)");
test=getchar();   // Now this will work

Check out this link for more info. It's for fgets, but it's the same problem with getchar()

Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177
0

Compare test value with '\n', like this sample:

int main() {
    int test;
    test = getchar();
    printf("[%d]\n", test);
    if(test == '\n') printf("Enter pressed.\n");
    return(0);
}

ps: your test must be int.

Jeffmagma
  • 452
  • 2
  • 8
  • 20
  • getchar() returns a char, and since '\0' is a char there's no reason to use an int. OP wasn't saying her test was failing, just for the first iteration of the loop. – Mike Sep 17 '12 at 19:14
  • 1
    @Mike The `getchar()` from the C standard library returns an `int`. It can't return a `char` since it must be able to distinguish an `EOF` condition from a valid character. – Daniel Fischer Sep 17 '12 at 19:22
0

Of course it skips the first reading, you put it in an if statement like so: if(test=='\n')

You got all the info for that certain student and then the user pressed enter, so you go back up to for(count=0;count<5;count++) and ask for new input for a new student. I think what you wanted to do is use a while statement instead.

Yarneo
  • 2,922
  • 22
  • 30
0

You could replace

> test=getchar();
>     if(test=='\n')
>     {
>         printf("Invalid input. Would you like to continue? (Y/N)");
>         test=getchar();
>     }

with

while((test=getchar()) == '\n')
{
  printf("Invalid input. Would you like to continue? (Y/N)");
}
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89