Thread: Assignment involving loops, if statements and arrays; program will not run

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    14

    Assignment involving loops, if statements and arrays; program will not run

    Hi, my program basically records the grades of 5 students in 3 subjects and prints the results according to total marks, percentage and what that percentage translates to in the form of a letter grade. Also it states the class, roll number, section and of the student. As I said, the program stops working after I try to enter the first value for class. Any help would be greatly appreciated, than you very much for your time.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    int main()
    {
        float mth[5],bio[5],chem[5],tm[5],p[5];
        char name[5][25],sec[5];
        int i,c[5],r[5];
        for (i=0;i<5;i++)
        {
            for (i=0;i<5;i++)
            {
                printf("Please enter your class, role number, section and name:  ");
                scanf("%d\n%d\n%c\n%s",c[i],r[i],sec[i],name[i]);
            }
            top:
            printf ("Please enter obtained marks for math, biology and chemistry for Student %d: ",i);
            scanf ("%f%f%f",&mth[i],&bio[i],&chem[i]);
            if ((mth[i]<0)||(mth[i]>100)||(bio[i]<0)||(bio[i]>100)||(chem[i]<0)||(chem[i]>100))
            {
                printf("Entry error, please try again.");
                goto top;
            }
            tm[i]=(mth[i]+bio[i]+chem[i]);
            p[i]=tm[i]/3;
    
    
        }
        for(i=0;i<5;i++)
        {
             printf("\nTotal Marks: %f\n \nPercent: %f\n",tm[i],p[i]);
             if (p[i]>=90)
              {
             printf("\nLetter Grade: A\n");
              }
             else if ((p[i]>=80)&&(p[i]<90)) {
             printf("\nLetter Grade: B\n");
             }
             else if ((p[i]>=70)&&(p[i]<80)) {
             printf("\nLetter Grade: C\n");
             }
             else if ((p[i]>=60)&&(p[i]<70)) {
             printf("\nLetter Grade: D\n");
             }
             else {
             printf("\nLetter Grade: E\n");
             }
        }
        return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1st I would try removing all the \n in your scanf format string and replace with spaces.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You use a go to statement?? This is not good style of coding i think

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by napalmgrenade View Post
    Code:
    ....
        char name[5][25],sec[5];
       
                scanf("%d\n%d\n%c\n%s",c[i],r[i],sec[i],name[i]);
    name is a 2-d array!!!!Maybe you should try to create an array of char*
    Last edited by std10093; 07-09-2012 at 07:44 AM.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by hk_mp5kpdw View Post
    1st I would try removing all the \n in your scanf format string and replace with spaces.
    I agree.Try also to remember this short example
    Code:
    .. 
    int n;
    scanf("%d",&n);
    ..

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    So what i suggest is this :
    Code:
    int main()
    {
        float mth[5],bio[5],chem[5],tm[5],p[5];
        char* name[5];
        char temp[20]={0},sec[5];
        int i,c[5],r[5];
        for (i=0;i<5;i++)
        {
            for (i=0;i<5;i++)
            {
                printf("Please enter your class, role number, section and name:  ");
                scanf("%d ",&(c[i]));
                scanf("%d ",&(r[i]));
                scanf("%c",&(sec[i]));
                scanf("%s",temp);
                
                name[i]=(char*)malloc(strlen(temp));
                strcpy(name[i],temp);
            }
    .......
    Also note that you use same variable(i) in the two for statements under the declaration of the variables.I think that this is not what you wish to do.I suggest using a second variable for the second loop(e.g. j),because with only one variable,the variable i,the following message is going to be appeared only once at your screen
    Code:
    printf ("Please enter obtained marks for math, biology and chemistry for Student %d: ",i);
    I believe that you want this to be asked for every student.

    Sorry for the mutliple quotes:/
    Last edited by std10093; 07-09-2012 at 08:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While statements and loops
    By Native in forum C Programming
    Replies: 18
    Last Post: 10-27-2010, 03:11 PM
  2. Functions and loops assignment problem
    By JFonseka in forum C Programming
    Replies: 2
    Last Post: 08-07-2007, 03:21 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Replies: 1
    Last Post: 05-26-2006, 08:13 AM
  5. Assignment Statements
    By BlueAerith in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2005, 12:09 PM