Thread: Prob with my gpa calculator program.

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    46

    Prob with my gpa calculator program.

    This is a gpa calculator....this code is working 100% but if i use for example course1=getchar(); and so on with the rest of the courses...the prog only waits for me to input for the first course while it skips the other courses...in addition to that the getchar() which makes the program not end if i just open the EXE file and not from the cmd it doesnt make any difference although it did with me when i wrote other progs.thanks
    Code:
    #include<stdio.h>
    void switcher(float *x){
         if(*x>=90.0)
         *x=12.0;
         else if(*x>=85.0)
              *x=11.0;
         else if(*x>=80.0)
              *x=10.0;
         else if(*x>=75.0)
              *x=9.0;
         else if(*x>=70.0)
              *x=8.0;
         else if(*x>=65.0)
              *x=7.0;
         else if(*x>=60.0)
              *x=6.0;
         else if(*x>=55.0)
              *x=5.0;
         }
    #define max 4
    main(){
           float *p1,*p2,*p3,*p4,*p5,*p6,course[6];/*i made a pointer to each course so that in order to make the reference call*/
           int i;
           float sum,totalgpa;
           float gpa[max]={3.94,2.88,3.83};/*i initiated the first 3 elements because these are the past semesters*/
           printf("\nHello Amr\n");
           printf("\nMaths 4 : ");
           scanf("%f",&course[0]);
           printf("\nDigital Logic Design : ");
           scanf("%f",&course[1]);
           printf("\nElectronic Devices 1 : ");
           scanf("%f",&course[2]);
           printf("\nElectrical Circuits 2 : ");
           scanf("%f",&course[3]);
           printf("\nMeasurements : ");
           scanf("%f",&course[4]);
           printf("\nScientific Thinking : ");
           scanf("%f",&course[5]);
           p1=course;
           p2=&course[1];
           p3=&course[2];
           p4=&course[3];
           p5=&course[4];
           p6=&course[5];
           switcher(p1);
           switcher(p2);
           switcher(p3);
           switcher(p4);
           switcher(p5);
           switcher(p6);
           sum=0.0;
           for(i=0;i<6;i++)
           sum+=course[i];       
           gpa[max-1]=sum/18;
           sum=0.0;
           for(i=0;i<max;i++)
           sum+=gpa[i];
           totalgpa=sum/4;
           printf("\nThe term's GPA is %1.2lf\n", gpa[max-1]);
           printf("\nTotal GPA is %1.2lf\n", totalgpa);
           }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
           p1=course;
           p2=&course[1];
           p3=&course[2];
           p4=&course[3];
           p5=&course[4];
           p6=&course[5];
           switcher(p1);
           switcher(p2);
           switcher(p3);
           switcher(p4);
           switcher(p5);
           switcher(p6);
           sum=0.0;
           for(i=0;i<6;i++)
           sum+=course[i];       
           gpa[max-1]=sum/18;
    Well you can get rid of all your pointers, and simplify to
    Code:
           sum=0.0;
           for(i=0;i<6;i++) {
             switcher( &course[i] );
             sum+=course[i];       
           }

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    i agree with Salem's suggestion on the pointers issue, your method is like trying to kill an ant with a sledgehammer! still, to answer your question, if i'm interpretting it correctly, you are having problems with the input buffer.

    am i correct in understanding that you were you using getchar in place of scanf for all your results entries?

    if so, the problem lies with the fact that when you hit enter (to enter your specific result) the computer also reads in the newline character '\n'. input in c is buffered, so that this newline character remains in the input buffer - so the next time you call getchar, it sees the newline character and thinks that you hit enter, the same for any more repeated calls to getchar ()

    you notice that you don't get that problem with scanf, the details of why, i couldn't tell you because i don't know - scanf stores the newline character as well, but perhaps repeated calls to scanf ignore what is already in the buffer - getchar doesnt. that is why putting getchar at the end of your program so that you can read the output of your program is not working.

    simply put, scanf and getchar don't really belong together at all, the only way to make them co-exist is to have some way of removing that newline character. this is done by flushing the input buffer - and i refuse to suggest a way to do this because it is a very hot topic of controversy in this forum. instead, i'll show you a way around the issue.

    you just want to be able to see the output for a few seconds right? if you include stdlib.h, there is a function called Sleep which stops your program for a specified amout of time. here's how i suggest you modify your program:

    Code:
    #include <stdio.h>
    #include <stdio.h>  /*you'll need this*/
    
    /*declare your function as normal*/
    
    int main ()
    {
        /*your program*/
        
        .
        .
        .
        .
        .
        
        Sleep (5000); /*case sensitive - causes program to 
                      wait for 5000 mili-seconds (ie 5 seconds)*/
        return 0;
    }
    you should probably look up the faq for details about flushing the input buffer, because i guarantee that you will encounter the problem again and again, and from the methods there are to deal with it, you should one yourself. hope this lot helps you out

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no standard (ANSI C) sleep function. That is specific to your compiler, if it has one at all.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  2. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  3. Prob with my GPA calculator
    By KidMan in forum C Programming
    Replies: 2
    Last Post: 09-23-2005, 01:55 AM
  4. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  5. proofread my expos-ay
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-04-2002, 08:31 PM