954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Scanf doesn't work? HELP!! Reading in wrong things... Homework Due tonight!

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;
}
crewxp
Newbie Poster
8 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

The problem is that you are leaving the 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();
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Genius! Thanks Dragon! Works perfectly :)

crewxp
Newbie Poster
8 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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

kbshibukumar
Junior Poster in Training
65 posts since Jan 2009
Reputation Points: 12
Solved Threads: 8
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>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?

kbshibukumar
Junior Poster in Training
65 posts since Jan 2009
Reputation Points: 12
Solved Threads: 8
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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++

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You