My logic must be ALL wrong , help on Average.

Reply

Join Date: Jun 2005
Posts: 3
Reputation: simplex85 is an unknown quantity at this point 
Solved Threads: 0
simplex85 simplex85 is offline Offline
Newbie Poster

My logic must be ALL wrong , help on Average.

 
0
  #1
Jun 19th, 2005
I am doind a lab for my class and the guidlines is suppoes to consist of an input file with student names and 10 quiz grades. The output file is suppose to produce the same format as the input file but one extra column, and that extra column is suppose to represent the average of their quiz grades.

here's my input file named input1.txt:

Dill James 13 18 17 15 19 20 20 16 14 19

King Damon 18 16 11 19 12 12 13 15 18 14

Thomas Dan 13 14 12 15 18 19 19 19 12 20


  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. void calculatescore();
  10.  
  11. int main ()
  12.  
  13.  
  14. {
  15.  
  16. string studentname;
  17. int total;
  18. int q1,q2, q3, q4, q5, q6, q7 ,q8 ,q9 ,q10;
  19. int average;
  20. ifstream infile;
  21. ofstream outfile;
  22.  
  23. infile.open("input1.txt");
  24. outfile.open("output.txt");
  25.  
  26.  
  27. outfile << fixed << showpoint << setprecision(2);
  28.  
  29.  
  30. outfile << left << setw(20) << "Student Name"
  31. << setw(1) << q1
  32. << setw(1) << q2
  33. << setw(1) << q3
  34. << setw(1) << q4
  35. << setw(1) << q5
  36. << setw(1) << q6
  37. << setw(1) << q7
  38. << setw(1) << q8
  39. << setw(1) << q9
  40. << setw(1) << q10
  41. << setw(1) << average;
  42.  
  43.  
  44.  
  45. while (infile >> studentname);
  46. {
  47. {
  48.  
  49. calculatescore();
  50. average = total / 10;
  51. }
  52. outfile << left << setw(10) << studentname
  53. << setw(1) << q1
  54. << setw(1) << q2
  55. << setw(1) << q3
  56. << setw(1) << q4
  57. << setw(1) << q5
  58. << setw(1) << q6
  59. << setw(1) << q7
  60. << setw(1) << q8
  61. << setw(1) << q9
  62. << setw(1) << q10
  63. << setw(1) << average;
  64.  
  65.  
  66. }// end while
  67. infile >> studentname>> q1>> q2>> q3>> q4>> q5
  68. >> q6 >> q7 >> q8 >> q9 << q10;
  69.  
  70. infile.close();
  71. outfile.close();
  72.  
  73.  
  74.  
  75. return 0;
  76.  
  77.  
  78. }
  79.  
  80.  
  81.  
  82. void calculatescore()
  83. {
  84. int total;
  85. int num = q1, q2, q3, q4, q5, q6, q7, q8 ,q9, q10;
  86. string studentname;
  87. int counter;
  88. ifstream infile;
  89.  
  90.  
  91.  
  92. infile >> studentname >> q1 >> q2 >> q3 >> q4 >> q5
  93. >> q6 >> q7 >> q8 >> q9 >> q10;
  94.  
  95.  
  96.  
  97. counter = 0;
  98. while (counter < 10)
  99. {
  100. total = 0;
  101. infile >> num;
  102. while (num > 0);
  103. {
  104. total = total + num;
  105. counter++;
  106. infile >> num;
  107. }
  108.  
  109. }
  110.  
  111. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: crazypitukillo is an unknown quantity at this point 
Solved Threads: 0
crazypitukillo crazypitukillo is offline Offline
Newbie Poster

Re: My logic must be ALL wrong , help on Average.

 
0
  #2
Jun 19th, 2005
Are necessary using the functions on the <iostream.h> header? I prefer the functions in <stdio.h> for files management. With these functions I present this code that realize your lab job.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAXSTR 80
  5.  
  6. int main ()
  7. {
  8.  
  9. FILE *fpin, *fpout;
  10. int q1, q2, q3, q4, q5, q6, q7 ,q8 ,q9 ,q10;
  11. float average;
  12. char str[MAXSTR + 1], firstname[MAXSTR], secondname[MAXSTR];
  13.  
  14. if ((fpin = fopen ("input1.txt", "r")) == NULL) {
  15. fprintf (stderr, "Error reading input1.txt");
  16. exit (EXIT_FAILURE);
  17. }
  18. if ((fpout = fopen ("output.txt", "w")) == NULL) {
  19. fprintf (stderr, "Error writing output.txt");
  20. exit (EXIT_FAILURE);
  21. }
  22.  
  23. while (1) {
  24. fgets (str, MAXSTR, fpin);
  25. if (feof (fpin))
  26. break;
  27. sscanf (str, "%s %s %d %d %d %d %d %d %d %d %d %d", firstname, secondname,
  28. &q1, &q2, &q3, &q4, &q5, &q6, &q7, &q8, &q9, &q10);
  29. average = (q1 + q2 + q3 + q4 + q5 + q6 + q7 + q8 + q9 + q10)/10.0;
  30. fprintf (fpout, "%s %s %d %d %d %d %d %d %d %d %d %d %.2f\n", firstname, secondname,
  31. q1, q2, q3, q4, q5, q6, q7, q8, q9, q10,
  32. average);
  33. }
  34. fclose (fpin);
  35. fclose (fpout);
  36.  
  37. return EXIT_SUCCESS;
  38. }
All lines in input1.txt file must ended with a carriage return for this code work fine. (sorry for my little english )
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: kreitcher is an unknown quantity at this point 
Solved Threads: 0
kreitcher kreitcher is offline Offline
Newbie Poster

Re: My logic must be ALL wrong , help on Average.

 
0
  #3
Jun 19th, 2005
I think it would be more elegant if you use functions to acheive certain key points in your program. Hey crazypitukillo, u have done the whole thing using C. I think simple x85 wants the whole thing in C++. Am I right?
-kreitcher
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 8
Reputation: kreitcher is an unknown quantity at this point 
Solved Threads: 0
kreitcher kreitcher is offline Offline
Newbie Poster

Re: My logic must be ALL wrong , help on Average.

 
0
  #4
Jun 19th, 2005
hey simplex85, why dont u use an array instead of declaring 10 variables in a row? You will also be able to loop efficiently and hence make ur program look better.
-kreitcher
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: crazypitukillo is an unknown quantity at this point 
Solved Threads: 0
crazypitukillo crazypitukillo is offline Offline
Newbie Poster

Re: My logic must be ALL wrong , help on Average.

 
0
  #5
Jun 19th, 2005
Kreitcher, think You that's always is necessary to program in C++? why is this more elegant? I think that depend of the problem to solve, but this is my point and I respect other view point. In this particular case, I prefer functions in the header <stdio.h> to work with files because are simpler to me than functions in iostream.h Respect to manage arrays, I do not use them because I think that the proffesor no seen yet
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: My logic must be ALL wrong , help on Average.

 
0
  #6
Jun 19th, 2005
> why is this more elegant?
C-style I/O doesn't recognize the std::string class, so you end up having to jump through error prone hoops to get it to work, or you need to use C-style strings, which are inherently error prone. That alone is reason to use iostreams. If you don't understand how to use iostreams, or you like C-style I/O better, that's okay. But please don't try to convince other people to take a big step backward in safe programming practice when they're still learning the basics.

@simplex85:

You have issues with your program. First, you try to print uninitialized variables. That results in undefined behavior, and you really want to avoid undefined behavior. Since printing out the scores when you haven't read any is nonsensical, you can remove that part of the program.

> while (infile >> studentname);
Notice the trailing semicolon. That means that the loop will read names until there's an input error, or the end of the file is reached. In other words, it's not doing what you think it is. Remove the semicolon.
  1. {
  2. calculatescore();
  3. average = total / 10;
  4. }
There's no need for braces here.

I don't know why you thought that calculatescore does anything meaningful. You would be better off avoiding functions until you know how to make them communicate with main.

> int q1,q2, q3, q4, q5, q6, q7 ,q8 ,q9 ,q10;
When you number variables like this, it's a sure sign that an array would be better.

Here's your program with the requisite fixes (off the top of my head, sorry for any errors):
  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. int average(int scores[10]);
  7.  
  8. int main()
  9. {
  10. std::string name, temp;
  11. int scores[10];
  12. std::ifstream in("infile");
  13. std::ofstream out("outfile");
  14.  
  15. if (!in || !out) {
  16. std::cerr << "File open failure\n";
  17. std::exit(EXIT_FAILURE);
  18. }
  19.  
  20. while (in >> name) {
  21. in >> temp;
  22. name += ' ' + temp;
  23. out << name << ' ';
  24. for (int i = 0; i < 10; i++) {
  25. in >> scores[i];
  26. out << scores[i] << ' ';
  27. }
  28. out << average(scores) << '\n';
  29. }
  30. }
  31.  
  32. int average(int scores[10])
  33. {
  34. int sum = 0;
  35.  
  36. for (int i = 0; i < 10; i++)
  37. sum += scores[i];
  38.  
  39. return sum / 10;
  40. }
Yea, it's ugly. But impromptu code tends to be ugly, and it should be more than enough to help you along.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: crazypitukillo is an unknown quantity at this point 
Solved Threads: 0
crazypitukillo crazypitukillo is offline Offline
Newbie Poster

Re: My logic must be ALL wrong , help on Average.

 
0
  #7
Jun 19th, 2005
Dogtree, I find that it is necessary to leave that the programmer is an intelligent being and that he will make the best desición in case you show him alternative of as making the things. I am not saying that simplex85 changes C++ for C, I am simply showing him another way to make the things in this particular case. For example, do you agree on hiding the programmer the "infamous" instruction goto? or do you prefer to tell him the advantages and disadvantages of such an instruction? There is a difference between to impose points of view and to let people to make her own decisions. The programming is one of the fields where can arrive to the same thing for very different roads
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: My logic must be ALL wrong , help on Average.

 
0
  #8
Jun 19th, 2005
That's all well and good, but you didn't answer the original question. You just threw away the code given and posted a C-style solution with the implication that it was somehow better, without explaining why, when there are very good arguments for avoiding such a solution until one knows what is gained and what is lost with the decision.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: simplex85 is an unknown quantity at this point 
Solved Threads: 0
simplex85 simplex85 is offline Offline
Newbie Poster

Re: My logic must be ALL wrong , help on Average.

 
0
  #9
Jun 20th, 2005
Well i'm taking C++ as a summer class and stuff is moving by real quick and i'm tryin my best to stay on course and some of this stuff you guys talk about, i am unfamiliar with. And yes this is a C++ program, i really like to thank you guys for the help and input.


Like i am not familiar with "std::" or what an array is yet, atleast for this lab, my professor does have certain guidelines. I'll try to work with the input you guys gave me.

But i am having the most trouble just tryin to figure out on how to read the input file and make changes to them properly.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: simplex85 is an unknown quantity at this point 
Solved Threads: 0
simplex85 simplex85 is offline Offline
Newbie Poster

Re: My logic must be ALL wrong , help on Average.

 
0
  #10
Jun 20th, 2005
I am getting an error about undeclared variables for q1 in my void calculatescore() function.

I guess what i just realize my problem is how do I assign the numbers in my input file properly in my code to be able to calculate them? So what exactl is the logic behind it? Cuz this book seems to assume that i should know more.


here's my input1.txt file:

Dill James 13 18 17 15 19 20 20 16 14 19

King Damon 18 16 11 19 12 12 13 15 18 14

Thomas Dan 13 14 12 15 18 19 19 19 12 20



  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. void calculatescore();
  10.  
  11. int main ()
  12.  
  13.  
  14. {
  15.  
  16. string studentname;
  17. int total;
  18. int q1,q2, q3, q4, q5, q6, q7 ,q8 ,q9 ,q10;
  19. int average;
  20. ifstream infile;
  21. ofstream outfile;
  22.  
  23. infile.open("input1.txt");
  24. outfile.open("output.txt");
  25.  
  26. void calculatescore();
  27.  
  28. outfile << fixed << showpoint << setprecision(2);
  29.  
  30.  
  31. outfile << left << setw(20) << "Student Name"
  32. << setw(1) << q1
  33. << setw(1) << q2
  34. << setw(1) << q3
  35. << setw(1) << q4
  36. << setw(1) << q5
  37. << setw(1) << q6
  38. << setw(1) << q7
  39. << setw(1) << q8
  40. << setw(1) << q9
  41. << setw(1) << q10
  42. << setw(1) << average;
  43.  
  44.  
  45.  
  46. while (infile >> studentname)
  47. {
  48.  
  49.  
  50. calculatescore();
  51. average = total / 10;
  52.  
  53. outfile << left << setw(10) << studentname
  54. << setw(1) << q1
  55. << setw(1) << q2
  56. << setw(1) << q3
  57. << setw(1) << q4
  58. << setw(1) << q5
  59. << setw(1) << q6
  60. << setw(1) << q7
  61. << setw(1) << q8
  62. << setw(1) << q9
  63. << setw(1) << q10
  64. << setw(1) << average;
  65.  
  66.  
  67. }// end while
  68. infile >> studentname>> q1>> q2>> q3>> q4>> q5
  69. >> q6 >> q7 >> q8 >> q9 >> q10;
  70.  
  71. infile.close();
  72. outfile.close();
  73.  
  74.  
  75.  
  76. return 0;
  77.  
  78.  
  79. }
  80.  
  81.  
  82.  
  83. void calculatescore()
  84. {
  85. int total;
  86.  
  87. //****this is where it says i have an error
  88. int num = q1, q2, q3, q4, q5, q6, q7, q8 ,q9, q10;
  89. string studentname;
  90. int counter;
  91. ifstream infile;
  92.  
  93.  
  94.  
  95. infile >> studentname >> q1 >> q2 >> q3 >> q4 >> q5
  96. >> q6 >> q7 >> q8 >> q9 >> q10;
  97.  
  98.  
  99.  
  100. counter = 0;
  101. while (counter < 10)
  102. {
  103. total = 0;
  104. infile >> num;
  105. while (num > 0);
  106. {
  107. total = total + num;
  108. counter++;
  109. infile >> num;
  110. }
  111.  
  112. }
  113.  
  114. }
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