473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help saving additional data to file?

I need to be able to save my additional data to my .dat file. The additional data is not part of the vector though. It is data I made after the vector. I have to save the finalGrade and letter grade to my file.Also, how can I save my letter grade to my file when I didn't actually name it that?

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <iomanip>
  7. #include <cmath>
  8. #include <vector>
  9. #include <algorithm>
  10. using namespace std;
  11.  
  12. class Grade
  13. {
  14. private:
  15.     string name;
  16.     double examOne, examTwo, homework, finalExam, finalGrade;
  17.  
  18.  
  19. public:
  20.     Grade(){setGrade("Tom", 94, 92, 87 ,85);};
  21.     Grade(string nm, double exOne, double exTwo, double hw, double final){setGrade(nm, exOne, exTwo, hw, final);};
  22.     void setGrade(string nm, double exOne, double exTwo, double hw, double final){name=nm; examOne=exOne; examTwo=exTwo; homework=hw; finalExam=final;};
  23.     string getName(){return name;};
  24.     double getExamOne(){return examOne;};
  25.     double getExamTwo(){return examTwo;};
  26.     double getHomework(){return homework;};
  27.     double getFinalExam(){return finalExam;};
  28.     double getFinalGrade(){return finalGrade;};
  29. };
  30.  
  31. int _tmain(int argc, _TCHAR* argv[])
  32. {
  33.     const int Grades = 2;
  34.     string filename = "grades.dat";
  35.     string name;
  36.     ofstream outFile;
  37.     int i; 
  38.     double finalGrade, examOne, examTwo, homework, finalExam;;
  39.     Grade g;
  40.  
  41.     vector<Grade> gTable;
  42.  
  43.     outFile.open(filename.c_str());
  44.  
  45.  
  46.     for(i=0; i<Grades; i++)
  47.     {
  48.         cout <<"\nEnter students name, 1st exam grade, 2nd exam grade, homework average,"
  49.              <<  "and final exam grade (type done to exit): \n";
  50.         cin >>name>>examOne>>examTwo>>homework>>finalExam;
  51.         if(name=="done")
  52.             break;
  53.  
  54.         g=Grade(name, examOne, examTwo, homework, finalExam);
  55.         gTable.push_back(g);
  56.  
  57.  
  58.     }
  59.  
  60.  
  61.  
  62.     cout << fixed << setprecision(2) << endl;
  63.     cout << "Student    Exam 1     Exam 2     Homework     Final Exam     Final     Letter"<<endl;
  64.     cout << "Name       Grade      Grade      Average      Grade          Grade     Grade"<<endl;
  65.     cout << "-------    ------     ------     --------     ----------     -----     -----"<<endl;
  66.  
  67.  
  68. for ( i = 0; i < Grades; i++ ) {
  69.   double finalGrade =
  70.       0.20 * gTable[i].getExamOne()
  71.     + 0.20 * gTable[i].getExamTwo()
  72.     + 0.35 * gTable[i].getHomework()
  73.     + 0.25 * gTable[i].getFinalExam();
  74.  
  75.   cout<< setw(7) << gTable[i].getName()
  76.       << setw(10) << gTable[i].getExamOne()
  77.       << setw(11) << gTable[i].getExamTwo()
  78.       << setw(13) << gTable[i].getHomework()
  79.       << setw(15) << gTable[i].getFinalExam()
  80.       << setw(10) << finalGrade;
  81.  
  82.   if ( finalGrade > 90 )
  83.     cout<< setw( 11 ) <<"A";   
  84.   else if ( finalGrade > 80 )
  85.     cout<< setw( 11 ) <<"B";   
  86.   else if ( finalGrade > 70 )
  87.     cout<< setw( 11 ) <<"C";    
  88.   else if ( finalGrade > 59 )
  89.     cout<< setw( 11 ) <<"D";   
  90.   else
  91.     cout<< setw( 11 ) <<"F"; 
  92.  
  93.  outFile<<gTable[i].getName()<<" "
  94.                 <<gTable[i].getExamOne()<<" "
  95.                 <<gTable[i].getExamTwo()<<" "
  96.                 <<gTable[i].getHomework()<<" "
  97.                 <<gTable[i].getFinalExam()<<" "
  98.                 <<gTable[i].getFinalGrade()<<endl;
  99.   cout<<'\n'<<endl;
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.     outFile.close();
  107.     cout << "The file " << filename
  108.          << " has been successfully written." << endl;
  109.  
  110.     cin.ignore();cin.ignore();
  111.  
  112.     return 0;
  113. }
Nov 8 '07 #1
1 1547
sicarie
4,677 Expert Mod 4TB
I need to be able to save my additional data to my .dat file. The additional data is not part of the vector though. It is data I made after the vector. I have to save the finalGrade and letter grade to my file.Also, how can I save my letter grade to my file when I didn't actually name it that?
I'm a little confused by what you mean "you didn't actually name it that". Can you not use the name you named it as?
Expand|Select|Wrap|Line Numbers
  1. outFile.open(filename.c_str());
  2.  
You are trying to open the file for reading and writing, am I correct? It looks like you do not specify that in the open() function, using ios::in | ios::out after the filename.
Nov 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
2
by: John Grogan | last post by:
I have absolutely no experience in Javascript although am a programmer by trade. I have a problem in a third-party system, as follows. The system uses "web" forms to capture and save data. ...
4
by: ralphNOSPAM | last post by:
Is there a function or otherwise some way to pull out the target text within an XML tag? For example, in the XML tag below, I want to pull out 'CALIFORNIA'. ...
2
by: Mike | last post by:
Greetings, Having a major problem here. running version 8.2 on win2003 server. The problem I am having is backing up a database seems to get to the last part of the backup and then fails. This...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
1
by: M Keeton | last post by:
I currently have a picture which is stored in a "System.Drawing.Image" variable and I want to save it as a bitmap file. I have tried 2 different approaches and both give me the following error: ...
5
by: TheGanjaMan | last post by:
Hi everyone, I'm trying to write up a simple image stamper application that stamps the Exif date information from the jpegs that I've taken from my digital camera and saves the new file with the...
7
by: Niyazi | last post by:
Hi, I am developing small insurance application using VB.NET and SQL server 2000. My tables in SQL server are: tbl_Customer (stores the custmer information) tbl_CustImage ...
2
by: =?Utf-8?B?SnJ4dHVzZXIx?= | last post by:
I just started using Windows Live OneCare, I had been using Norton, but was unable to fix the problems I was having. I have yet been unsuccessful with OneCare as well. I keep getting the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.