Saturday 15 May 2010

c++ - Counting variables have high initial values? -


I want to count the letters of the file, grade.txt:

  ABADACAFABCDFFFACDCCBBACBACCCBBAAAAADBACAFFBBCCDAABBFFAACCBBAACCCCBB < / Code> 

Code here:

  Include # lt; Iostream & gt; / * Includes header file to input and output / / #include & lt; Fstream & gt; / * Contains a library so that you can read or write files * / #include & lt; Iomanip & gt; / * Contains a library that allows formatting * / namespace std to be used; / * Standard A / B, C, D, F = 0, operating from the main integral * / integer; // the number of classes of each class x; // Storage store is being read outstream outfill; Outfile.open ("out.txt"); // output file ifstream infile; Infile.open ("grade.txt"); // w / grade file while reading (infile> gt; & gt; x) {// file switch for every grade (x) {case 'A': A ++; // increase the count for whatever grade reading brakes; Case 'B': B ++; break; Case 'C': C ++; break; Case 'D': D ++; break; Case 'F': F ++; break; Default: cout & lt; & Lt; "Invalid category"; }} Outfile & lt; & Lt; "Calculate each letter grade \" & lt; & Lt; Endl; // Output Results Outfile & lt; & Lt; "A:" & lt; & Lt; A & lt; & Lt; "B:" & lt; & Lt; B & L; & Lt; "C:" & lt; & Lt; C & lt; & Lt; "D:" & lt; & Lt; D & lt; & Lt; "F:" & lt; & Lt; F; Cout & lt; & Lt; "A:" & lt; & Lt; A & lt; & Lt; "B:" & lt; & Lt; B & L; & Lt; "C:" & lt; & Lt; C & lt; & Lt; "D:" & lt; & Lt; D & lt; & Lt; "F:" & lt; & Lt; F; System ("pause"); / * Console window "wait" | * / Return 0; } / * Main function of the end * /  

The following is my output:

  calculation of grade A of each letter: 169 B: 2686848 C: 18 D: 5 F: 8  

I do not have my address why the imposition of life 'a' and 'b' has such high counts. When I debug, they start at extremely high values, and then walk normally.

Line

  int a, b, c, d, f = 0;  

equals

  int a; Int b; Int c; Int d; Int f = 0;  

In other words, a , b , c , and d Has been refined.

You can use

  int a = 0, b = 0, c = 0, d = 0, f = 0 can fix it;  

or

  int a = 0; Int b = 0; Int c = 0; Int d = 0; Int f = 0;  

No comments:

Post a Comment