| | |
C++ program write and display to file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
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
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)
#include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <iomanip> #include <cmath> #include <TCHAR.H> #include <vector> #include <algorithm> using namespace std; class Grade { private: string name; int exam1, exam2, homework, finalExam; public: Grade(){setGrade("Tom", 94, 92, 87 ,85);}; Grade(string name, int ex1, int ex2, int hw, int final){setGrade(name, ex1, ex2, hw,final);} void setGrade(string nm, int ex1, int ex2, int hw, int final){name=nm; exam1=ex1; exam2=ex2; homework=hw; finalExam=final;}; string getName(){return name;}; int getExam1(){return exam1;}; int getExam2(){return exam2;}; int getHomework(){return homework;}; int getFinalExam(){return finalExam;}; }; int _tmain(int argc, _TCHAR* argv[]) { const int Grades = 5; string filename = "grades.dat"; string name; ofstream outFile; int i, exam1, exam2, homework, finalExam; Grade g; vector<Grade> gTable; outFile.open(filename.c_str()); if(outFile.fail()) { cout << "The file was not successfully opened."<<endl; exit(1); } outFile << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(2); for(i=0; i<Grades; i++) { cout <<"\nEnter students name, 1st exam grade, 2nd exam grade, homework average, and final exam grade (type done to exit): \n"; cin >> name>>exam1>>exam2>>homework>>finalExam; if(name=="exit") break; cout <<"\nFor the students the following data has been written to the file: \n"; cout <<name<<" "<<exam1<<" "<<exam2<<" "<<homework<<" "<<finalExam<<endl; g=Grade(name, exam1, exam2, homework, finalExam); gTable.push_back(g); } for(i=0; i<gTable.size(); i++) { outFile <<gTable[i].getName()<<" " <<gTable[i].getExam1()<<" " <<gTable[i].getExam2()<<" " <<gTable[i].getHomework()<<" " <<gTable[i].getFinalExam()<<endl; } outFile.close(); cout << "The file " << filename << " has been successfully written." << endl; cin.ignore();cin.ignore(); return 0; }
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...
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
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?
>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?
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?
I'm here to prove you wrong.
•
•
Join Date: Oct 2007
Posts: 49
Reputation:
Solved Threads: 1
•
•
•
•
>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.
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.
![]() |
Similar Threads
- Help with Java program writing (Java)
- Read/write to same file > once, Help (C++)
- read data from file (C++)
- write value of pointer into file (C)
- write to text file using fprintf() (C)
- module to get video file dimensions (Python)
- Program Output Display Tweak Help (Java)
- Error trying to write to existing file (C)
- How to write a header file (C++)
Other Threads in the C++ Forum
- Previous Thread: Request for a good Visual C++ Tutorial?
- Next Thread: Printing numbers in ascending order (help)
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






