Thread: A little bit of a problem

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    2

    A little bit of a problem

    I got a problem. When the fucntion DataEntry finished its job, it wont go back to main menu instead it go to the while part, which is Yes or No. Can someone help me find my mistake here?

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    #define SIZE 7
    
    
    typedef struct GPA
    {
        char courseCode [20];
        int credits;
        float grade;
    } ST;
    
    
    int DataEntry (ST gpa [SIZE], int i);
    void SearchEdit (ST gpa [SIZE], int i);
    float CalGrade (ST gpa [SIZE], float grade, int i);
    void Display (ST gpa [SIZE], int i, float grade);
    
    
    
    
    int main (void)
    {
        int i = 0, ans;
        ST gpa [SIZE];
        float grade;
        char res, ans1;
    
    
        system ("cls");
    
    
    
    
        do
        {
            printf ("MAIN MENU\n");
            printf ("1. Add Course Code, Credits and Grades\n");
            printf ("2. Search and Edit a Course\n");
            printf ("3. Calculate the GPA and Display the Courses\n");
            printf ("4. Exit\n\n");
            printf ("Your Choice: ");
            scanf ("%d", &ans);
    
    
    
    
    
    
            switch(ans)
            {
                case 1:i=DataEntry (gpa, i);
                        break;
                case 2: SearchEdit (gpa, i);
                        break;
                case 3: CalGrade (gpa, grade, i);
                        Display;
                        break;
                case 4:
                        exit(0);
                        break;
                default: printf ("Wrong Choice");
    
    
            }
    
    
            printf ("\n\nRepeat (Y/N)? ");
            scanf ("%c", &res);
    
    
        }
    
    
        while(res=='y'||res=='Y');
        getch();
    
    
        return 0;
    }
    
    
    int DataEntry (ST gpa [SIZE],int i)
    {
        char sub[20], ans1;
        int cred, j;
        float gra;
    
    
        printf ("\n");
        printf ("You can only input up to 7 courses.");
        printf ("\nHow many courses: ");
        scanf ("%d", &j);
    
    
        for (i=0; i<j; i++)
        {
            fflush (stdin);
            printf ("Enter Course Code: ");
            gets(sub);
            strcpy (gpa[i].courseCode, sub);
            printf ("Enter Credits: ");
            scanf ("%d",&cred);
            gpa[i].credits = cred;
            printf ("Enter Grade: ");
            scanf ("%.1f", &gra);
            gpa[i].grade = gra;
        }
    
    
        return i;
    
    
    }
    
    
    void SearchEdit (ST gpa[SIZE], int i)
    {
        char seco[20], newCourse[20];
        int newCred, j;
        float newGrade;
    
    
        system ("cls");
        printf ("Course to search: ");
        gets (seco);
    
    
        for (j=0; j<i; j++)
            if (strcmp (gpa [j].courseCode, seco)==0)
            {
                printf ("FOUND!/n/n");
                printf ("Enter new grade: ");
                scanf ("%.1f", &newGrade);
                gpa[j].grade = newGrade;
            }
            else
                printf ("No Records Found!");
        getch();
        system("CLS");
    }
    
    
    float CalGrade (ST gpa [SIZE], float grade, int i)
    {
        float gr = 0, grr, cred=0;
        int j;
    
    
        for (j=0; j<i; j++)
        {
            grr = gpa[j].credits*gpa[j].grade;
            gr+=grr;
        }
    
    
        for (j=0; j<i; j++)
        {
            cred+=gpa[j].credits;
        }
        grade = gr/cred;
    
    
        return grade;
    }
    
    
    void Display (ST gpa [SIZE], int i, float grade)
    {
        int j;
    
    
        system ("cls");
        printf ("Course Code\t\tCredits\nGrade\n\n");
        for (j=0;j<i;j++)
        {
            printf("%s\t\t",gpa[j].courseCode);
            printf("%d\t%.1f\n"), gpa[j].credits, gpa[j].grade;
        }
    
    
        printf ("Your GPA is %.1f.", grade);
        getch();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that the newline that is left in the input buffer from the input in DataEntry is read by scanf with "%c". What you can do is to discard the characters remaining in the input buffer at the end of DataEntry. You did this with fflush(stdin), but that actually results in undefined behaviour. One solution is to simply have a loop that reads and discards characters until a '\n' is detected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    2
    Holy sh..t! Im sorry I didnt saw that one. Im just new to programming and tired of making this one. Hahaha! Thank you sire.


    Edit: Didnt work sir.
    Last edited by JJ Achay Dahili; 02-07-2015 at 03:43 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should post your updated code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM