1

What is wrong in this program?? I am trying to figure out since 2 days but no help at all!! String output is only after string input and after selecting choice, default string input is new line character by default i guess. Besides if i type string while inputting choice, it shows me the name output by default. Here is my code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct marks {
    char subject[15];
    int mark;
};
struct student {
    char name[10];
    int roll;
    struct marks m[3];
};
void displayData(struct student);
int displayChoice();
struct student getNewRecord();
int main() {
    struct student s[5];
    int count = 0;
    int choice;
    do{
        choice = displayChoice();
        if(choice == 1){
            s[count] = getNewRecord();
            count ++;
        }
        else if(choice == 4)
            break;
        else
            printf("Invalid choice");
    }while(1);
}
struct student getNewRecord() {
    struct student temp;
    printf("Enter your Name : ");
    fgets(temp.name, 10,stdin   );
    printf("Your name is : %s",temp.name);
    return temp;

}
int displayChoice() {
    int choice;
    printf("\n\nPlease select your choice :\n");
    printf("1. Add new Record\n");
    printf("2. Display All data \n");
    printf("3. Remove last Record\n");
    printf("4. Exit the program\n");
    printf("What is your choice : \n");
    scanf("%d", &choice);
    return choice;
}
void displayData(struct student s){
    printf("Your name : %s", s.name);
}

And here are some screen shots: enter image description here

I don't know and doesn't have any idea about what is going wrong. Please Help me.. Thanks in advance..

MD XF
  • 7,860
  • 7
  • 40
  • 71
  • Simplest fix: `char dummy; scanf("%d%c", &choice, &dummy);` Your `scanf` is reading the integer value and leave the `'\n'` (the ASCII value of enter keyboard button) car into `stdin` buffer. So you have to spool it before to read something new. – LPs Sep 28 '16 at 12:42
  • It fixed the first problem. When i input saugat for choice it displays the output showing your name is saugat.. how do i fix it? – Saugat Sigdel Sep 28 '16 at 12:48
  • I'm not getting you. Could you elaborate? Your code is supposed to output `printf("Your name is : %s",temp.name);` just after the name is input. – LPs Sep 28 '16 at 12:55
  • My code displays (What is Your choice : ) at that instant don't enter a number just type a string... it displays the name instead of executing if/else condition it doesn't function properly... please help me.. – Saugat Sigdel Sep 28 '16 at 12:57
  • You cannot input a string if `scanf` format specifier is `%d`. You should check the return value of `scanf` to check if the it fails. – LPs Sep 28 '16 at 13:01

1 Answers1

0

You have some problems into your code:

  1. scanf leave the '\n' char into stdin and the next call to a function that reads from stdin exits immediately due to that character.
  2. You should check the return value of scanf to be sure that the user entered a valid number.
  3. Do not use old style function declaration with empty parameters. If no parameters are required use (void)
  4. Your endless main loop should take care of s array size.

You can add a empty function for stdin like:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct marks {
    char subject[15];
    int mark;
};

struct student {
    char name[10];
    int roll;
    struct marks m[3];
};

void displayData(struct student);
int displayChoice(void);
struct student getNewRecord();
void flush_stdin(void);

int main(void)
{
    struct student s[5];
    size_t count = 0;
    int choice;

    do{
        choice = displayChoice();

        if(choice == 1)
        {
            s[count] = getNewRecord();
            count ++;
        }
        else if(choice == 4)
            break;
        else
            printf("Invalid choice");
    }
    while ((count < sizeof(s)/sizeof(s[0]) ) && (choice != 4));
}

struct student getNewRecord(void)
{
    struct student temp;

    printf("Enter your Name : ");

    scanf("%s", temp.name);

    printf("Your name is : %s", temp.name);

    flush_stdin();

    return temp;
}

int displayChoice(void) {
    int choice;

    printf("\n\nPlease select your choice :\n");
    printf("1. Add new Record\n");
    printf("2. Display All data \n");
    printf("3. Remove last Record\n");
    printf("4. Exit the program\n");
    printf("What is your choice : \n");

    if (scanf("%d", &choice) != 1)
    {
        choice = 99;
    }

    flush_stdin();

    return choice;
}

void displayData(struct student s)
{
    printf("Your name : %s", s.name);
}

void flush_stdin(void)
{
    int c;
    while ( ((c = getchar()) != '\n') && (c != EOF) );
}
LPs
  • 16,045
  • 8
  • 30
  • 61
  • 1
    Sorry, I triggered on the name, didn't read the code. My 5ct: I actually prefer e,g, "drop" or similar ("emtpy" is fine, too). – too honest for this site Sep 28 '16 at 13:50
  • On what base, pray tell, did you, whoever *you* is, downvote that code? With the exception of the forgotten return from `main` we would go into nitpicking territory quite rapidly and nits are not good reasons for a downvote, or are they? – deamentiaemundi Sep 28 '16 at 14:10
  • @deamentiaemundi I don't care. Hater gonna hate ;). – LPs Sep 28 '16 at 14:16