Sign in with
Sign up | Sign in
Your question
Closed

C++ reading/output files

Last response: in Applications
Share
October 2, 2010 5:06:56 PM

Hey back again, okay so the program is to read in a file that has 10 students names with 5 test scores for each of them. The file looks like this:

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 74 63

Then I am suppose to write it to a new file which is suppose to look like this:

Student Test1 Test2 Test3 Test4 Test5 Average Grade
( the names and test scores with the average and the average turned into a letter grade)


I started off with like 35+ errors, got it down to like 2, well here's the program and the errors:


  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. void calcAvg(ifstream& in_file, ofstream& out_file, float& avg);
  8. char calc_grade(float avg);
  9.  
  10. int main() {
  11.  
  12. ifstream inp;
  13. ofstream outp;
  14.  
  15. inp.open("Ch07_Ex09Date.txt");
  16. outp.open("Ch07_Ex09OUT.txt");
  17.  
  18. outp << "Student Test1 Test2 Test3 Test4 Test5 Average Grade";
  19.  
  20. string name;
  21. float classAvg = 0;
  22. char grade;
  23. int k;
  24. float avg;
  25.  
  26. for (k=1; k<=10; k++)
  27. {
  28. inp >> name;
  29. outp << setw(10) << name;
  30.  
  31.  
  32. calcAvg(inp, outp, avg);
  33. outp << setw(6) << avg;
  34. classAvg = classAvg + avg;
  35. grade = calc_grade(avg);
  36. outp << setw(5) << " " << grade << endl;
  37. }
  38.  
  39. classAvg = classAvg / 10;
  40. outp << endl << "Class Average = " << classAvg;
  41.  
  42.  
  43. return 0;
  44. }
  45.  
  46. void calcAvg(ifstream& inp, ofstream& outp, float& avg)
  47. {
  48. int score, k;
  49. float sum = 0.0;
  50.  
  51. for (k=1; k<=5; k++)
  52. {
  53. inp >> score;
  54. outp << setw(4) << score << " ";
  55. sum = sum + score;
  56. }
  57. avg = sum / 5;
  58. }
  59.  
  60. char calc_grade(total)
  61. {
  62.  
  63. if (total >= 0)
  64.  
  65. if (total >= 60)
  66.  
  67. if (total >= 70)
  68.  
  69. if (total >= 80)
  70.  
  71. if (total >= 90)
  72.  
  73. if (total > 100)
  74. cout << "Grade can not exceed 100" << endl;
  75. else
  76. cout << "A" << endl;
  77. else
  78. cout << "B" << endl;
  79. else
  80. cout << "C" << endl;
  81. else
  82. cout << "D" << endl;
  83. else
  84. cout << "F" << endl;
  85. else
  86. cout << "Grade can not be a negative number" << endl;
  87. }







ERRORS:

1>c:\users\welcome back\documents\visual studio 2008\projects\file\file\file input.cpp(59) : error C2065: 'total' : undeclared identifier
1>c:\users\welcome back\documents\visual studio 2008\projects\file\file\file input.cpp(60) : error C2448: 'calc_grade' : function-style initializer appears to be a function definition

More about : reading output files

a b L Programming
October 2, 2010 6:30:32 PM

In line 60 you have forgottent to type the variable "total". It should read "float total".

This will leave one error. You declare "calc_grade" as of type "char" but do not return a value.
Score
0
October 2, 2010 7:26:06 PM

ohh ok,yea i was like I thought when I put total as the parameter it was declaring it but I guess i forgot i need to say what kind of variable it is.

How would I return the letter grade? It wouldn't be return total right because that would just return the number grade right?


hmmm would it be instead of COUTing the letter grade I would set them to a value like grade = 'A' for a char then return grade?

  1. else
  2. grade = 'A';
  3. else
  4. grade = 'B';
  5. else
  6. grade = 'C';
  7. else
  8. grade = 'D';
  9. else
  10. grade = 'F';
  11.  
  12.  
  13. return grade;
Score
0
Related resources
October 2, 2010 7:37:38 PM

okay here's the new version, it runs but right away shows me this error, with attach a image of it, but here's the code:

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. void calcAvg(ifstream& in_file, ofstream& out_file, float& avg);
  8. char calc_grade(float avg);
  9.  
  10. int main() {
  11.  
  12. ifstream inp;
  13. ofstream outp;
  14.  
  15. inp.open("Ch07_Ex09Date.txt");
  16. outp.open("Ch07_Ex09OUT.txt");
  17.  
  18. outp << "Student Test1 Test2 Test3 Test4 Test5 Average Grade";
  19.  
  20. string name;
  21. float classAvg = 0;
  22. char grade;
  23. int k;
  24. float avg;
  25.  
  26. for (k=1; k<=10; k++)
  27. {
  28. inp >> name;
  29. outp << setw(10) << name;
  30.  
  31.  
  32. calcAvg(inp, outp, avg);
  33. outp << setw(6) << avg;
  34. classAvg = classAvg + avg;
  35. grade = calc_grade(avg);
  36. outp << setw(5) << " " << grade << endl;
  37. }
  38.  
  39. classAvg = classAvg / 10;
  40. outp << endl << "Class Average = " << classAvg;
  41.  
  42.  
  43. return 0;
  44. }
  45.  
  46. void calcAvg(ifstream& inp, ofstream& outp, float& avg)
  47. {
  48. int score, k;
  49. float sum = 0.0;
  50.  
  51. for (k=1; k<=5; k++)
  52. {
  53. inp >> score;
  54. outp << setw(4) << score << " ";
  55. sum = sum + score;
  56. }
  57. avg = sum / 5;
  58. }
  59.  
  60. char calc_grade(float total)
  61. {
  62. char letter;
  63.  
  64. if (total >= 0)
  65.  
  66. if (total >= 60)
  67.  
  68. if (total >= 70)
  69.  
  70. if (total >= 80)
  71.  
  72. if (total >= 90)
  73.  
  74. if (total > 100)
  75. cout << "Grade can not exceed 100" << endl;
  76. else
  77. letter = 'A';
  78. else
  79. letter = 'B';
  80. else
  81. letter = 'C';
  82. else
  83. letter = 'D';
  84. else
  85. letter = 'F';
  86. else
  87. cout << "Grade can not be a negative number" << endl;
  88.  
  89. return letter;
  90. }



ERROR:

Score
0
a b L Programming
October 2, 2010 7:48:38 PM

The dialog is arising becasue you haven't assigned any value to "letter" in the cases that "total" is negative or >100. The console shows that in this case "total" was negative (this may be indicative of another bug).

In line 62, when you declare "letter" assign some value to it. Then that value will be returned in the two error cases. E.g.

62. char letter = ' ';

Now you just need to determine why "total" was negative. In cases like this I'd recommend using the debugger to single-step through the program, or parts of it, and watch what values are assigned to the variables. It should be pretty obvious when things go wrong.
Score
0
October 2, 2010 8:20:45 PM

ohh okay, ya my prof. showed us how to do it line by line but i totally forgot, ctrl+f5 debugs but what is the hotkey for line to line debug?
Score
0
a b L Programming
October 2, 2010 8:31:55 PM

Have a look at this article which gives a pretty good guide to debugging in Visual Studio. (The examples shown are C#, but the principles are the same.)
Score
0
October 3, 2010 5:26:46 PM

I think I found out how to do it but when I get to where i declared classAvg = 0.0 it is say the value is -1.0737418e+008. Everything is using like that type of value.... I am so confused, is my program wrong or something?
Score
0
a b L Programming
October 3, 2010 5:38:17 PM

No, I think that's just a normal floating-point error. Since floating-point numbers can't be represented exactly in a computer there are always going to be errors of this level of magnitude. (Although I'm surprised that it happens for 0.0.)

If it matters, it's always better to test whether A - B < 1e+007 (that's just an example) rather than testing whether A = B. In really important calculations (e.g. financial ones) you would use fixed-point numbers rather than floating-point ones to avoid this sort of error.
Score
0
October 4, 2010 2:56:39 AM

avg is never changing???? its value is always -8.5899347e+008.... is my file being read in?? and my grade is always 113 and the same value that is set in the declaring statement of 'q'.
Score
0
a b L Programming
October 4, 2010 6:07:55 AM

OOps. I misread the exponential as e-008 when you said e+008. That is not a rounding error!

I'm not quite sure without checking it out what's going wrong here. Have you tried single-stepping through calcAvg to see what's happening?

BTW, I would have defined calcAvg as a function of two parameters returning the value avg, but that's just a matter of choice.

I haven't got Visual C++ on this computer, but if you can't find the problem I'll fire up my other one and have a look.
Score
0

Best solution

a b L Programming
October 4, 2010 7:53:22 AM

OK, I've found your problem now. The input file is never being opened. It's down to the working directory when the program is being debugged. In my case that directory was:

C:\Users\Ian\Documents\Visual Studio 2010\Projects\test101\test101

You either need to put the input file in this directory or, if they are in another directory provide a full path name. So if you want the input and output files to be in C:\tmp (for instance), you need to open them as:

  1. inp.open("C:\\tmp\\Ch07_Ex09Date.txt" );
  2. outp.open("C:\\tmp\\Ch07_Ex09OUT.txt" );


This then produces an output file filled with figures. I'll leave it up to you to check whether the results are correct.

The value you were getting for avg was just a random one because you never initialized the variable. In the function calAvg the variable score was again uninitialized, so had the same value, which was then being assigned to avg. It's good practice to initialize all variable to some known value; some compilers automatically initialize variables to 0, but most will just leave some random value in the variable.
Share
October 4, 2010 2:05:47 PM

Haha yes it works, yea i remembered in my first C++ class we just glanced at files but we put ours on our flashdrive and we had to specify the full path of the flashdrive for the file to be found, just totally forgot about that. THANK YOU!!!! now just gotta figure out how to get everything nice and neat inside the file.
Score
0
October 4, 2010 2:33:38 PM

Best answer selected by laserpp.
Score
0
October 4, 2010 4:31:20 PM

This topic has been closed by Mousemonkey
Score
0
!