943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1711
  • C++ RSS
Mar 4th, 2009
0

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

Expand Post »
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!!


C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crewxp is offline Offline
8 posts
since Feb 2009
Mar 4th, 2009
0

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

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();
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Mar 5th, 2009
0

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

Genius! Thanks Dragon! Works perfectly
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crewxp is offline Offline
8 posts
since Feb 2009
Mar 5th, 2009
0

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

A general way is to flush the input stream (instead of using getchar()).
Use
Quote ...
fflush(stdin)
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 5th, 2009
1

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 5th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Narue ...
>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?
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 6th, 2009
0

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

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

And http://www.manpagez.com/man/3/fflush/
Quote ...
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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 6th, 2009
0

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

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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: VC2008 Permission denied
Next Thread in C++ Forum Timeline: How can i prevent Alt+F4, closing a dialog?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC