944,141 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5773
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 7th, 2007
0

C++ program write and display to file

Expand Post »
Hi, I have never used a forum like this before but I am stuck! I have to write a C++ program that allows the user to enter up to 20 students info. It must include Nem, Exam 1 grade, exam 2 grade, Homework average, final exam average. For each student, the program should first calculate a final grade using the formula:
Final grade = 0.20 * exam 1 grade + 0.20 * exam 2 grade + 0.35 * Home work avearge + 0.25 * final exam grade and then assign a letter grde - All information should then be displayed and written to a file - I have been having a real tough time with programming for some reason - Everyone in my class just seems to get itand I am really struggling - Well anyway this is what I have so far and If anyone knows of soem good resources to help me learn this class better other than the text book I would appreictae the information! Thanks
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <cmath>
  7. #include <TCHAR.H>
  8. #include <vector>
  9. #include <algorithm>
  10. using namespace std;
  11.  
  12. class Grade
  13. {
  14. private:
  15. string name;
  16. int exam1, exam2, homework, finalExam;
  17.  
  18. public:
  19. Grade(){setGrade("Tom", 94, 92, 87 ,85);};
  20. Grade(string name, int ex1, int ex2, int hw, int final){setGrade(name, ex1, ex2, hw,final);}
  21. void setGrade(string nm, int ex1, int ex2, int hw, int final){name=nm; exam1=ex1; exam2=ex2; homework=hw; finalExam=final;};
  22. string getName(){return name;};
  23. int getExam1(){return exam1;};
  24. int getExam2(){return exam2;};
  25. int getHomework(){return homework;};
  26. int getFinalExam(){return finalExam;};
  27. };
  28.  
  29. int _tmain(int argc, _TCHAR* argv[])
  30. {
  31. const int Grades = 5;
  32. string filename = "grades.dat";
  33. string name;
  34. ofstream outFile;
  35. int i, exam1, exam2, homework, finalExam;
  36. Grade g;
  37. vector<Grade> gTable;
  38.  
  39. outFile.open(filename.c_str());
  40. if(outFile.fail())
  41. {
  42. cout << "The file was not successfully opened."<<endl;
  43. exit(1);
  44. }
  45.  
  46. outFile << setiosflags(ios::fixed)
  47. << setiosflags(ios::showpoint)
  48. << setprecision(2);
  49.  
  50. for(i=0; i<Grades; i++)
  51. {
  52. cout <<"\nEnter students name, 1st exam grade, 2nd exam grade, homework average, and final exam grade (type done to exit): \n";
  53. cin >> name>>exam1>>exam2>>homework>>finalExam;
  54. if(name=="exit")
  55. break;
  56.  
  57.  
  58. cout <<"\nFor the students the following data has been written to the file: \n";
  59. cout <<name<<" "<<exam1<<" "<<exam2<<" "<<homework<<" "<<finalExam<<endl;
  60.  
  61. g=Grade(name, exam1, exam2, homework, finalExam);
  62. gTable.push_back(g);
  63. }
  64.  
  65. for(i=0; i<gTable.size(); i++)
  66. {
  67. outFile <<gTable[i].getName()<<" "
  68. <<gTable[i].getExam1()<<" "
  69. <<gTable[i].getExam2()<<" "
  70. <<gTable[i].getHomework()<<" "
  71. <<gTable[i].getFinalExam()<<endl;
  72. }
  73. outFile.close();
  74. cout << "The file " << filename
  75. << " has been successfully written." << endl;
  76.  
  77. cin.ignore();cin.ignore();
  78.  
  79. return 0;
  80. }
Last edited by WaltP; Nov 7th, 2007 at 10:40 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mandsmom is offline Offline
5 posts
since Nov 2007
Nov 7th, 2007
0

Re: C++ program write and display to file

Once we cot CODE tags in your post, you have some very nicely formatted code!

I don't see where your equation has been implemented. Can you figure out where it should go?
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Nov 7th, 2007
0

Re: C++ program write and display to file

I believe it needs to go around line 21 or so - But really I am not sure - i have written a code to just calculate grades before but since I had to get into the writing to a file - I have completely confused myself!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mandsmom is offline Offline
5 posts
since Nov 2007
Nov 8th, 2007
0

Re: C++ program write and display to file

What's the purpose of the equation? What function is responsible for this feature? That's where it goes... Somewhere around 21 is correct.
Last edited by WaltP; Nov 8th, 2007 at 12:03 am.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Nov 8th, 2007
0

Re: C++ program write and display to file

I'm seeing a lot of the same code from supposedly different people.
Last edited by Narue; Nov 8th, 2007 at 10:28 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 8th, 2007
0

Re: C++ program write and display to file

If they are cheating, at least, they could cheat together
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vallelente_82 is offline Offline
2 posts
since Nov 2007
Nov 8th, 2007
0

Re: C++ program write and display to file

I do not appreciate the last two comments and I have not seen any code similar to mine - If I were cheating I would at least pick one that is done - I am very new to this programming and the forum but i thought comments like that were not allowed - Also, I did take portions of my code from my text book if that is what you are referring to but i thought that was the point of having a text book for the class. If you cannot be helpful then why are you even replying?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mandsmom is offline Offline
5 posts
since Nov 2007
Nov 8th, 2007
0

Re: C++ program write and display to file

calm down, why beeing so seriously? smile and you will see the world WILL smile back at you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vallelente_82 is offline Offline
2 posts
since Nov 2007
Nov 8th, 2007
0

Re: C++ program write and display to file

>I do not appreciate the last two comments
Don't worry. Others will appreciate them.

>and I have not seen any code similar to mine
Here's one: http://www.daniweb.com/forums/thread95911.html

>If I were cheating I would at least pick one that is done
I don't know, I've seen some pretty lame attempts to cheat over the years.

>If you cannot be helpful then why are you even replying?
I also can't be helpful because you really haven't asked a proper question yet. You've mentioned a formula, said you're having trouble finishing the program, and posted the code. This suggests you want the work done for you, which we won't do. Walt pushed you in the right direction as much as he could without giving you code for the solution, so where's your updated attempt?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 8th, 2007
0

Re: C++ program write and display to file

Click to Expand / Collapse  Quote originally posted by Narue ...
>I do not appreciate the last two comments
Don't worry. Others will appreciate them.

>and I have not seen any code similar to mine
Here's one: http://www.daniweb.com/forums/thread95911.html

>If I were cheating I would at least pick one that is done
I don't know, I've seen some pretty lame attempts to cheat over the years.
LOL Narue you are my new idol. <3 and yes i appreciate it.
bums who want everything done for them are lose. This site helps out, a lot i might add. It a give take situation. They do not do the work for you. Read the stickys before posting and you will understand.
Reputation Points: 21
Solved Threads: 1
Light Poster
helixkod is offline Offline
49 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: Request for a good Visual C++ Tutorial?
Next Thread in C++ Forum Timeline: Printing numbers in ascending order (help)





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


Follow us on Twitter


© 2011 DaniWeb® LLC