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.

FirstInitial: NOTHING
LastInitial: T

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

HELP!!

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

int Sum_Total=0;
int Average_Highest=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 class0=0;
double ClassAvg[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';
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';
scanf("%c%c",&fi2,&li2);
printf("\nFIRST INITIAL:%c - LAST INITIAL:%c",fi2,li2);
printf("\n");
{
fi=fi2;
li=li2;
}
printf("\n");
}

{
li0=li;
fi0=fi;
class0=i;
}
ClassAvg[z]=(sum/t);
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);

return 0;
}``````

## 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.

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

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.