0

This program have some segmentation fault which is not solved by me please anyone can solve the program. I also include the string header file then also error comes.. What's the problem

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int num[5], i, j, k;

char name[50];
for (i = 0; i < 5; i++)
{
    printf("\nENTER THE NAME:");
    scanf("\n %s", &name[i]);
    printf("\nENTER THE PERCENTAGE:");
    scanf("\n %d", &num[i]);
}
printf("\nTHE STUDENT PASS IN EXAM IS:");
for (j = 0; j < 5; j++)
{
    if (num[j] >= 70)
    {
        printf("\n%s", name[j]);
    }
}

{
    printf("\nFAIL");
    for (k = 0; k < 5; k++)
    {
        if (num[k] < 70)
        {
            printf("\n%s", name[k]);
        }
    }
}

}

rene
  • 41,474
  • 78
  • 114
  • 152
  • relevant: https://stackoverflow.com/questions/2876357/determine-the-line-of-code-that-causes-a-segmentation-fault – rene Aug 31 '19 at 07:59
  • use 2 dimesional char arry vriable – jasinth premkumar Aug 31 '19 at 08:01
  • The title of this question is really bad, the body is probably even worse. What did you try to solve the issue? How can this post be useful or found by anyone with such a title? – Marco Bonelli Aug 31 '19 at 08:23
  • regarding: `scanf("\n %s", &name[i]);` and `scanf("\n %d", &num[i]);` 1) do not include a '\n' as a character in the format string. 2) always check the returned value ( not the parameter values ) to assure the operation was successful (it returns the number of successful input format conversion specifiers) 3) when using '%s' and/or '%[...]' always use a MAX CHARACTERS modifier that is one less than the length of the input buffer as those always append a NUL byte. This avoids any possibility of a buffer overflow and the attendant undefined behavior – user3629249 Sep 02 '19 at 14:11
  • regarding: `#include ` it is a poor programming practice to include header files those contents are not being used. Suggest removing that statement. – user3629249 Sep 02 '19 at 14:29
  • OT: The posted code contains some 'magic' numbers. 'magic' numbers are numbers with no basis. I.E. 5, 50, 70. 'magic' numbers make the code much more difficult to understand, debug. etc. Suggest using an 'enum' statement or `#define` statements to give those 'magic' numbers meaningful names, then using those meaningful names throughout the code. – user3629249 Sep 02 '19 at 14:33
  • OT: for ease of readability and understanding: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement. 2) please consistently indent the code. indent after EVERY opening brace '{'. unindent before every closing brace '}'. Suggest each indent level be 4 spaces. – user3629249 Sep 02 '19 at 14:35
  • OT: for ease of readability and understanding: 1) separate code blocks: `for` `if` `else` `while` `do...while` `switch` `case` `default` via a single blank line 2) separate functions via 2 or 3 (be consistent) blank lines – user3629249 Sep 02 '19 at 15:04

2 Answers2

0

from your code i see that you are assigning name to single variable

char nam[40]

if you give input it will store like

'e.g input="name" a[0]=n,a[1]=a,a[2]=m

so you cant ginve input more than 1 name and also it will replace 1st name with new one

my solution is to this will be using two dimensional variable

updated code:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int num[5], i, j, k;

char name[5][50];
for (i = 0; i < 5; i++)
{
    printf("\nENTER THE NAME:");
    scanf("\n %s", name[i]);
    printf("\nENTER THE PERCENTAGE:");
    scanf("\n %d", &num[i]);
}
printf("\nTHE STUDENT PASS IN EXAM IS:");
for (j = 0; j < 5; j++)
{
    if (num[j] >= 70)
    {
        printf("\n%s", name[j]);
    }
}

printf("\nFAIL");
for (k = 0; k < 5; k++)
{
    if (num[k] < 70)
    {
        printf("\n%s", name[k]);
    }
}
the busybee
  • 10,755
  • 3
  • 13
  • 30
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22
-1

The following proposed code:

  1. does not include header files that are not used 'conio.h' and 'string.h'
  2. properly checks for I/O errors
  3. uses meaningful names rather than 'magic' numbers for 5, 50. 70
  4. removes an unneeded set of braces '{' and '}'
  5. Makes use of a 2 dimensional array for the student names
  6. limits the scope of variables: 'i' 'k' 'j'
  7. uses a proper signature for function: main()
  8. follows the axiom: only one statement per line and (at most) one variable declaration per statement.
  9. separates code blocks (in this case the 'for()' code blocks) via a single blank line
  10. for ease of readability, includes a space inside parens, after semicolons, around C operators, etc
  11. uses a meaningful name 'grade' rather than the meaninless 'num'
  12. properly outputs error messages to stderr

and now, the proposed code:

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

#define MIN_PASSING_GRADE 70
#define MAX_STUDENTS  5
#define MAX_NAME_LEN  50

int main( void ) 
{
    int grade[ MAX_STUDENTS ];
    char name[ MAX_STUDENTS ][ MAX_NAME_LEN ];

    for ( int i = 0; i < MAX_STUDENTS; i++ )
    {
        printf( "\nENTER THE NAME:" );
        if( scanf( " %49s", name[i] ) != 1 )
        {
            fprintf( stderr, "failed to input student name\n" );
            exit( EXIT_FAILURE );
        }

        printf( "\nENTER THE PERCENTAGE:" );
        if( scanf( " %d", &grade[i] ) != 1 )
        {
            fprintf( stderr, "failed to input student grade\n" );
            exit( EXIT_FAILURE );
        }
    }

    printf( "\nTHE STUDENT PASS IN EXAM IS:" );
    for ( int j = 0; j < MAX_STUDENTS; j++ )
    {
        if ( grade[j] >= MIN_PASSING_GRADE )
        {
            printf( "\n%s", name[j] );
        }
    }

    printf( "\nFAIL" );
    for ( int k = 0; k < MAX_STUDENTS; k++ )
    {
        if ( grade[k] < MIN_PASSING_GRADE )
        {
            printf( "\n%s", name[k] );
        }
    }
}
user3629249
  • 16,402
  • 1
  • 16
  • 17