Tuesday, 25 June 2013

Input The Marks Obtained In 5 Subjects

As the name suggests, this program accepts the values of the marks obtained by a student and displays the total percentage of marks obtained and the division the student is placed in.

I wrote the a while back based on a question in Let Us C (4th) edition. Anyway, this program is memorable to me because after writing this I saw that I could change the background colour of the entire screen.
This was before I could use the gotoxy function, so it uses a lot of cls to rewrite the entire screen.

We just set the text and background colour and then cls the screen. If we do this once, everything else appears in the colours you set.


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <windows.h>

#define TRUE 1
#define FALSE !TRUE

int sub1,sub2,sub3,sub4,sub5,state,tfl,tfl2,ranks;
float pcm,total;
char c,d;

void SetColorAndBackground(int ForgC, int BackC);
float calculation(void);
int division(void);
main()
{

    state=FALSE;

while(!state)
{
    SetColorAndBackground(7,9);
    system("cls");
    tfl=12,tfl2=12;
        printf("Enter the marks obtained by the student\n");
        printf("English\t\tScience\t\tMath\tSocial Sc.\tGFC\n");
        scanf("%i",&sub1);
        system("cls");

        printf("Enter the marks obtained by the student\n");
        printf("English\t\tScience\t\tMath\tSocial Sc.\tGFC\n");
        printf("%i\t\t",sub1);
        scanf("%i",&sub2);
        system("cls");

        printf("Enter the marks obtained by the student\n");
        printf("English\t\tScience\t\tMath\tSocial Sc.\tGFC\n");
        printf("%i\t\t%i\t\t",sub1,sub2);
        scanf("%i",&sub3);
        system("cls");

        printf("Enter the marks obtained by the student\n");
        printf("English\t\tScience\t\tMath\tSocial Sc.\tGFC\n");
        printf("%i\t\t%i\t\t%i\t",sub1,sub2,sub3);
        scanf("%i",&sub4);
        system("cls");

        printf("Enter the marks obtained by the student\n");
        printf("English\t\tScience\t\tMath\tSocial Sc.\tGFC\n");
        printf("%i\t\t%i\t\t%i\t%i\t\t",sub1,sub2,sub3,sub4);
        scanf("%i",&sub5);
        system("cls");

        printf("Please Look At The Marks\n");
        printf("English\t\tScience\t\tMath\tSocial Sc.\tGFC\n");
        printf("%i\t\t%i\t\t%i\t%i\t\t%i",sub1,sub2,sub3,sub4,sub5);
        printf("\n\n\n\t\tAre The marks correct?(Y/N):");


        while(tfl==12)
        {
            c=toupper(getch());
            if(c=='Y')
            {
                system("cls");
                printf("Percentage of marks obtained is %.2f\n",calculation());
                ranks=division();
                if(ranks==1)
                {
                    printf("First Division.\n");
                }
                else if(ranks==2)
                {
                    printf("Second Division.\n");
                }
                else if(ranks==3)
                {
                    printf("Third Division.\n");
                }
                else if (ranks==5)
                {
                    printf("The marks are out of range for a division.\n");
                }
                else
                {
                    printf("Failed.\n");
                }
                printf("\n\nCalculate another?(Y/N): ");
                while(tfl2==12)
                {
                    d=toupper(getch());
                    if(d=='Y')
                    {
                        system("cls");
                        tfl=10;
                        tfl2=10;
                        break;
                    }
                    if(d=='N')
                    {
                        SetColorAndBackground(7,0);
                        system("cls");
                        exit(0);
                    }

                }
            }
            if(c=='N')
            {
                system("cls");
                break;
            }

        }
}

}

float calculation(void)
{
    total=sub1+sub2+sub3+sub4+sub5;
    pcm=total/5;
    return(pcm);
}

int division(void)
{
    int div;
    div=calculation();
    if(div>59 && div<101)
    {
        return(1);
    }
    else if(div>49 && div<60)
    {
        return(2);
    }
    else if(div>39 && div<50)
    {
        return(3);
    }
    else if(div>100 || div<1)
    {
        return(5);
    }
    else
    {
        return(4);
    }
}

void SetColorAndBackground(int ForgC, int BackC)
{
     WORD wColor=((BackC&0x0F)<<4)+(ForgC & 0x0F);;
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor);
     return;
}

No comments:

Post a Comment