Simple program but I am unsure how to fix it

Discussion in 'C' started by Beckyanne, Sep 29, 2006.

  1. Beckyanne

    Beckyanne New Member

    Joined:
    Sep 29, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I wonder if anyone can see where I'm going wrong? What I am trying to do is get a student name, get their two scores then it works out a grade (Pass,merit,fail) based on the addition of the two scores. Then it must loop 5 times to get 5 student names.

    On the first loop everything works fine, but when it gets to the second it will display "Fail" no matter what combination of numbers I put in. Also it will not display the error message if I go over 50 (on the second attempt - works fine on the first)

    I'm pretty new to this, so any helpful guidance would be greatly apprieciated :)

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void Welcome_Screen (void);
    void score (void);
    void Grade (void);
    
    
    int i;
    char name1[5][25], name2[5][25];
    int score1[5], score2[5], answer[5];
    
    main()
    {
    
    Welcome_Screen();
    clrscr();
    
    
    
    //This sets the counter to 0
    i=0;
    
    do
    {
    
    //Gets name and saves it
    printf("\n\nPlease enter the students forename - ");
    scanf("%c", name1[i]);
    printf("Please enter the students surname - ");
    scanf("%c", name2[i]);
    
    score();
    
    //increments the do loop by one until it has done it 5 times.
    i++;
    }
    while (i<5);
    clrscr();
    
    
    getch();
    return 0;
    }
    //-----------------------------------------------------------------
    void Welcome_Screen (void)
    {
    //Displays a welcome message on initialisation
    	printf("************************");
    	printf("\n Student Grading System");
    	printf("\n************************");
    	printf("\n\n\n\n\n\n\nPress any key to continue...");
    
    getch();
    }
    //------------------------------------------------------------------
    void score (void)
    {
     again:
    //Gets first score and saves it
    printf("Enter %c %c 's first score - ", name1[i], name2[i]);
    scanf("%d", score1);
    					if (score1[i] >50)
                   	{
                       printf("The score must not be above 50");
                       goto again;
                      }
    
    
    again2:
    //Gets second score and saves it
    printf("Enter %c %c 's second score - ", name1[i], name2[i]);
    scanf("%d", score2);
    					if (score2[i] >50)
                   	{
                       printf("The score must not be above 50/n");
                       goto again2;
                      }
    answer[i]= score1[i] + score2[i];
    //calls the Grade function
    Grade();
    
    getch();
    }
    //------------------------------------------------------------------
    void Grade (void)
    {
    	if (answer[i] <30)
    		{
    		printf("\nFinal grade for %c %c - Fail", name1[i], name2[i]);
          }
       else if (answer[i] <=60&& answer[i] >=30)
       	{
          printf("\nFinal grade for %c %c - Pass", name1[i], name2[i]);
          }
       else
       	{
          printf("\nFinal grade for %c %c - Merit", name1[i], name2[i]);
          }
    
    }
    
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    There are quite a few errors in the program. I am mentioning the one I just saw when I looked at it.

    1. scanf for string should be %s instead of %c.
    2. score function you are using the variable i which is never initialized.
    3. You are taking the input in score1 the scores but comparing with score1 where i is never initilaized.
    4. Same is the case in score1
    5. similar things are observed in grade function as wel.

    I think the above points will help you rectify the problem you are facing.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice