1

Why does my program close before taking the input for k and then displaying it.

I am writing a code for a menu based program so I need to take input from user after he has entered the information so I can have 1.Print names 2.Exit while doing this I realized my program didn't take the input and just skipped the part where it is supposed to take value of l from user. So trying to debug it I deleted stuff and came down to this simple program and realized it still wont work any idea why?

#include <stdio.h>

struct student
{
    char name[50];
    char lname[50];
    float marks;
} s[15];

int main ()
{
    int i, j,k;

    printf("Please enter the number of students:\n");
    scanf ("%d", &j);

    printf ("Please enter the information for students as asked.\n");

    for (i = 0; i < j; i++)
    {
        scanf ("%s %s %f\n", s[i].name, s[i].lname, &s[i].marks);
    }

    printf("Please enter a number\n");
    scanf ("%d", &k);

    printf("your number was %d", k);

    return 0; 
}
8
  • scanf ("%d", k); --> scanf ("%d", &k); Commented Oct 26, 2017 at 3:21
  • When debugging, did you use a debugger? With a debugger you can step through your code line by line to see what happens. I also recommend you take some time to read How to debug small programs by Eric Lippert. Commented Oct 26, 2017 at 3:24
  • My bad I missed the &k but it still wont solve the problem. Commented Oct 26, 2017 at 3:24
  • I didnt use any debugger I was trying to debug it based on what I know I dont know what debuggers are Ill try to see if I can use one for this program. Commented Oct 26, 2017 at 3:26
  • 2
    You need to learn how to use a debugger, it's one of the most important tools for a programmer. Commented Oct 26, 2017 at 4:00

2 Answers 2

1

scanf ("%s %s %f\n", s[i].name, s[i].lname, &s[i].marks);

should be

scanf ("%s %s %f", s[i].name, s[i].lname, &s[i].marks);

The \n in scanf just consumes newline char. It will continue consuming newline until a non-newline char is found, which is put back into stdin for the next IO operation

Sign up to request clarification or add additional context in comments.

2 Comments

I think maybe this is not the key to his question. Maybe he input char instead of float at the end.
You are right the new line wasnt needed however it still didnt solve my problem but I found an answer you can refer to the thread it looks like my struct definition wasnt as needed.
0

Try this Code

#include<stdio.h>
typedef struct student
{
char name[50];
char lname[50];
int mark;
}S;

int main ()
{
int i, j,k;

printf("Please enter the number of students:\n");
scanf ("%d", &j);

S record[j];

for (i = 0; i < j; i++)    {
   printf ("Please enter the information for %d student as asked.\n",i+1);

  scanf ("%s %s %f",record[i].name, record[i].lname, &record[i].mark);
}

printf("Please enter a number\n");
scanf ("%d", &k);

printf("your number was %d \n", k);

return 0;
}

You were declaring structure student array in the structure declaration itself.you have to declare the array in main function.

2 Comments

This worked can you explain on how that "S" works which we defined just after the structure ended I realize we defined the array once in the main program but what is "S" in S record[j]; Anyways thanks for your help!
See the Keyword "typedef". you can Give another name to your structure student using this keyword. S is the typedef name of structure Student. reefer this link - fresh2refresh.com/c-programming/c-typedef

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.