For some reason, I can't get scanf to work. I did it just like the book said. Try it. I'm supposed to enter (FirstInitial)(LastInitial)Grade
Ex: TB100 For Tom Brady got a 100.

It reads:
FirstInitial: NOTHING
LastInitial: T
Grade:0

Then it skips to the second student and did this
FirstInitial: B
LastInitial: NOTHING
Grade: 100

HELP!!

#include <stdio.h>
//#include <string.h>

int Sum_Total=0;
int Average_Highest=0;
int Grade_Max=0;
int Count_Total_Students=0;
int Classes[4];




int main(void)
{

        /*Each students. data consists of a first and last initials and
                            1 exam score (0..100)* */

//Create Classes
int i=0;
char fi0;
char li0;
double grade0=0;
double class0=0;
double ClassAvg[4]={0,0,0,0};

double ClassGrade[4]={0,0,0,0};
char fia[4];
char lia[4];

for (i=0;i<4;i++)
{
        double sum=0;
        printf("How many students are in class %d?\n",(i+1));
        int t=0;
        scanf("%d",&t);
        //int Students[t];

        char fi='a';
        char li='a';
        double grade=0;
        int z=0;
        for (z=0;z<t;z++)
        {
                printf("Enter student %d's data. Ex:TC100 (Tom Brady has a 100) :: ",(z+1));
                char fi2='a';
                char li2='a';
                double grade2=0;
                scanf("%c%c",&fi2,&li2);
                printf("\nFIRST INITIAL:%c - LAST INITIAL:%c",fi2,li2);
                printf("\n");
                scanf("%d",&grade2);
                if (grade2>grade)
                {
                        grade=grade2;
                        fi=fi2;
                        li=li2;
                }
                printf("\n");
                printf("FI:%c LI:%c GR:%d\n",fi2,li2,grade2);
                sum=sum+grade2;
        }

        if (grade>grade0)
        {
        grade0=grade;
        li0=li;
        fi0=fi;
        class0=i;
        }
        ClassAvg[z]=(sum/t);
        ClassGrade[z]=grade;
        fia[z]=fi;
        lia[z]=li;

}

//Computations
double TotalAvg=ClassAvg[0]+ClassAvg[1]+ClassAvg[2]+ClassAvg[3];
TotalAvg=TotalAvg/4;

printf("-CLASS CALCULATIONS-\nTotal Classes:4\nTotal Average of All Classes::%d\nHighest Grade was a %d made by %c.%c. in Class %d",TotalAvg,grade0,fi0,li0,class0);
printf("\n[CLASS1]\nClass Average:%d\nHighest Score by %c.%c.::%d",ClassAvg[0],fia[0],lia[0],ClassGrade[0]);
printf("\n[CLASS2]\nClass Average:%d\nHighest Score by %c.%c.::%d",ClassAvg[0],fia[1],lia[1],ClassGrade[1]);
printf("\n[CLASS3]\nClass Average:%d\nHighest Score by %c.%c.::%d",ClassAvg[0],fia[2],lia[2],ClassGrade[2]);
printf("\n[CLASS4]\nClass Average:%d\nHighest Score by %c.%c.::%d",ClassAvg[0],fia[3],lia[3],ClassGrade[3]);

return 0;
}

Recommended Answers

All 7 Replies

The problem is that you are leaving the <Enter> key in the keyboard after getting the number of students. After numeric input like that you have to flush the keyboard of the '\n' character.

double sum=0;
        printf("How many students are in class %d?\n",(i+1));
        int t=0;
        scanf("%d",&t);
        getchar();

Genius! Thanks Dragon! Works perfectly :)

A general way is to flush the input stream (instead of using getchar()).
Use

fflush(stdin)

>Use fflush(stdin)
Bad advice. fflush only has defined behavior for output streams. Passing an input stream is a one-way ticket to a broken program.

>Use fflush(stdin)
Bad advice. fflush only has defined behavior for output streams. Passing an input stream is a one-way ticket to a broken program.

Can you please explain?

http://c-faq.com/stdio/stdinflush.html

And http://www.manpagez.com/man/3/fflush/

The function fflush() forces a write of all buffered data for the given
output or update stream via the stream's underlying write function. The
open status of the stream is unaffected.

No mention of input streams here, so trying to flush an input stream is not going to be a good idea.

Sure, it might work for you, but the other guy is busy looking for the reinstall disks all of a sudden.
But we don't deal with "maybe, works for me" programming here.

Umm,
Well I just know don't why would I use the scanf() to read out a character when I have the pretty getchar() in hand.

char a
a=getchar();

While another thing to keep in mind (this is for you, the thread starter) is that all these functions are related to C rather than C++. So the C forum would have been a better place. But never mind. You are free to exploit the inter-compatibility of C and C++

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.