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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 8
Reputation: crewxp is an unknown quantity at this point 
Solved Threads: 0
crewxp crewxp is offline Offline
Newbie Poster

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

 
0
  #1
Mar 4th, 2009
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!!


  1. #include <stdio.h>
  2. //#include <string.h>
  3.  
  4. int Sum_Total=0;
  5. int Average_Highest=0;
  6. int Grade_Max=0;
  7. int Count_Total_Students=0;
  8. int Classes[4];
  9.  
  10.  
  11.  
  12.  
  13. int main(void)
  14. {
  15.  
  16. /*Each students. data consists of a first and last initials and
  17.   1 exam score (0..100)* */
  18.  
  19. //Create Classes
  20. int i=0;
  21. char fi0;
  22. char li0;
  23. double grade0=0;
  24. double class0=0;
  25. double ClassAvg[4]={0,0,0,0};
  26.  
  27. double ClassGrade[4]={0,0,0,0};
  28. char fia[4];
  29. char lia[4];
  30.  
  31. for (i=0;i<4;i++)
  32. {
  33. double sum=0;
  34. printf("How many students are in class %d?\n",(i+1));
  35. int t=0;
  36. scanf("%d",&t);
  37. //int Students[t];
  38.  
  39. char fi='a';
  40. char li='a';
  41. double grade=0;
  42. int z=0;
  43. for (z=0;z<t;z++)
  44. {
  45. printf("Enter student %d's data. Ex:TC100 (Tom Brady has a 100) :: ",(z+1));
  46. char fi2='a';
  47. char li2='a';
  48. double grade2=0;
  49. scanf("%c%c",&fi2,&li2);
  50. printf("\nFIRST INITIAL:%c - LAST INITIAL:%c",fi2,li2);
  51. printf("\n");
  52. scanf("%d",&grade2);
  53. if (grade2>grade)
  54. {
  55. grade=grade2;
  56. fi=fi2;
  57. li=li2;
  58. }
  59. printf("\n");
  60. printf("FI:%c LI:%c GR:%d\n",fi2,li2,grade2);
  61. sum=sum+grade2;
  62. }
  63.  
  64. if (grade>grade0)
  65. {
  66. grade0=grade;
  67. li0=li;
  68. fi0=fi;
  69. class0=i;
  70. }
  71. ClassAvg[z]=(sum/t);
  72. ClassGrade[z]=grade;
  73. fia[z]=fi;
  74. lia[z]=li;
  75.  
  76. }
  77.  
  78. //Computations
  79. double TotalAvg=ClassAvg[0]+ClassAvg[1]+ClassAvg[2]+ClassAvg[3];
  80. TotalAvg=TotalAvg/4;
  81.  
  82. 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);
  83. printf("\n[CLASS1]\nClass Average:%d\nHighest Score by %c.%c.::%d",ClassAvg[0],fia[0],lia[0],ClassGrade[0]);
  84. printf("\n[CLASS2]\nClass Average:%d\nHighest Score by %c.%c.::%d",ClassAvg[0],fia[1],lia[1],ClassGrade[1]);
  85. printf("\n[CLASS3]\nClass Average:%d\nHighest Score by %c.%c.::%d",ClassAvg[0],fia[2],lia[2],ClassGrade[2]);
  86. printf("\n[CLASS4]\nClass Average:%d\nHighest Score by %c.%c.::%d",ClassAvg[0],fia[3],lia[3],ClassGrade[3]);
  87.  
  88. return 0;
  89. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,440
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Mar 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 8
Reputation: crewxp is an unknown quantity at this point 
Solved Threads: 0
crewxp crewxp is offline Offline
Newbie Poster

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

 
0
  #3
Mar 5th, 2009
Genius! Thanks Dragon! Works perfectly
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

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

 
0
  #4
Mar 5th, 2009
A general way is to flush the input stream (instead of using getchar()).
Use
fflush(stdin)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #5
Mar 5th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

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

 
0
  #6
Mar 5th, 2009
Originally Posted by Narue View Post
>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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #7
Mar 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

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

 
0
  #8
Mar 6th, 2009
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.
  1. char a
  2. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC