| | |
Scanf doesn't work? HELP!! Reading in wrong things... Homework Due tonight!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 8
Reputation:
Solved Threads: 0
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!!
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!!
C++ Syntax (Toggle Plain Text)
#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; }
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(); Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
A general way is to flush the input stream (instead of using getchar()).
Use
Use
•
•
•
•
fflush(stdin)
http://c-faq.com/stdio/stdinflush.html
And http://www.manpagez.com/man/3/fflush/
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.
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.
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.
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++
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.
C++ Syntax (Toggle Plain Text)
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++
Last edited by siddhant3s; Mar 6th, 2009 at 10:34 am.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
![]() |
Other Threads in the C++ Forum
- Previous Thread: VC2008 Permission denied
- Next Thread: Getting a Processes PID From Image Name/Dropping Files
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






