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; 
}
Azeem
  • 11,148
  • 4
  • 27
  • 40
Tyrion
  • 13
  • 4
  • `scanf ("%d", k);` --> `scanf ("%d", &k);` – Sergey Kalinichenko Oct 26 '17 at 03: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](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert. – Some programmer dude Oct 26 '17 at 03:24
  • My bad I missed the &k but it still wont solve the problem. – Tyrion Oct 26 '17 at 03: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. – Tyrion Oct 26 '17 at 03:26
  • `"%s %s %f\n"` --> `"%s %s %f"` (Or You made an incorrect input as a number.) – BLUEPIXY Oct 26 '17 at 03:30
  • 2
    You need to learn how to use a debugger, it's one of the most important tools for a programmer. – Barmar Oct 26 '17 at 04:00
  • See [Trailing blank in `scanf()` format string](http://stackoverflow.com/questions/19499060/) for why you don't use one. You never check that `scanf()` succeeded. Most likely, the final `scanf()` fails because you have a non-numeric character (not a white space character either) in the input, and it fails to convert it. – Jonathan Leffler Oct 26 '17 at 06:10
  • You can also clarify what you input to use your program. It can be of great help for us. – chris Oct 26 '17 at 06:55

2 Answers2

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

qwn
  • 347
  • 3
  • 15
  • I think maybe this is not the key to his question. Maybe he input char instead of float at the end. – chris Oct 26 '17 at 06:54
  • 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. – Tyrion Oct 26 '17 at 22:42
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.

Angel
  • 112
  • 15
  • 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! – Tyrion Oct 26 '17 at 22:44
  • 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 - https://fresh2refresh.com/c-programming/c-typedef/ – Angel Oct 27 '17 at 04:25