is this wromg

This is a discussion on is this wromg within the C Programming forums, part of the General Programming Boards category; i have a code here and i havent done c inalong time is this wrong Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> main() ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    is this wromg

    i have a code here and i havent done c inalong time is this wrong
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    main()
    {
    char scores[10][5]; // i just edit this
     int i,j;
     for(i=0;i<10;i++)
     {
         printf("enter student %i",i+1);
         scanf("%s",scores[i][j]);  // is this wrong should i just scanf score[i] only
             
             for(j=0;j<5;j++)
             {
             printf("enter the score");
             scanf("%i",&scores[i][j]);
     
              }
      }
    
    
    
    
     getch();
    
    }
    Last edited by mouse666666; 02-15-2010 at 04:18 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's wrong because:

    a) You're using %s for a number.
    b) You don't need that first scanf call because you should be just using the inner loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User ungalnanban's Avatar
    Join Date
    Feb 2010
    Location
    Chenai
    Posts
    12

    Thumbs up

    Yes, your code has small logical mistake.

    first scanf is for getting the student name, so scores[i] is enough but you have used

    Code:
    scanf("%s",scores[i][j]);
    See the following code.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    main()
    {
    char scores[10][5]; // i just edit this
     int i,j;
     for(i=0;i<5;i++)
     {
         printf("\nEnter student %i",i+1);
         scanf("%s",scores[i]);  // is this wrong should i just scanf score[i] only
    
             for(j=0;j<2;j++)
             {
             printf("\n\t\tenter the score");
             scanf("%i",&scores[i][j]);
    
              }
      }
    
    }
    Output:

    Code:
    Enter student 1 s1
                    enter the score 70
                    enter the score 45
    Enter student 2 s2
                    enter the score 55
                    enter the score 78
    Enter student 3 s3
                    enter the score 89
                    enter the score 78
    Enter student 4 s4
                    enter the score 78
                    enter the score 90
    Enter student 5 s5
                    enter the score 90
                    enter the score 80

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    How do you want to frame your data structure , since once after you stored their marks,
    you'ill definitely lose your student name, please explain it better

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: is this wromg

    For your requirement you can use structure. I think your idea is that to store 10 students marks.
    try this code.

    Code:
    struct students
    {
     char name[80];
     int marks[5];
    };
    
    
    #include<stdio.h>
    #include<stdlib.h>
    main()
    {
    
            struct students s[10];
     int i,j;
     for(i=0;i<10;i++)
     {
         printf("enter student %i:",i+1);
         scanf("%s",s[i].name);  // is this wrong should i just scanf score[i] only
    
             for(j=0;j<5;j++)
             {
             printf("enter the score:");
             scanf("%i",&s[i].marks[j]);
              }
      }
    
     for(i=0;i<10;i++)
     {
            printf("Student %d: %s\n",i+1,s[i].name);
            for(j=0;j<5;j++)
                    printf("\tmarks %d:%d\n",j+1,s[i].marks[j]);
            printf("\n\n");
     }
    }

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    %s in scanf should use width specifier to prevent memory overrun

    like this:

    Code:
    char name[80];
    scanf("%79s", name);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21