File Input and ouput homework

All shorts of lost. PLEASE HELP!!

A local college stores information about students pursuing a degree in Computer Information Systems in a
file named cisdegree.txt (a sample is included with this laboratory assignment). Each line in the text
file includes a student’s identification number (eight digits), last name, first name, middle initial, grade point
average, and a single character representing one of the following degree options:
A – applications N – networking P – programming W – web development
An example line:
10203846 Hart Bartholomew S 3.27 P
The college wants the data from this file divided into four separate files, one for each degree program –
appmajors.txt, netmajors.txt, progmajors.txt, and webmajors.txt – with each file
listing the students in that particular major. Since each file will only include the students from one major,
you don’t need to include the character for major. For example, the aforementioned student would be
included in the file progmajors.txt with the following line:
10203846 Hart Bartholomew S 3.27

^^that is the background part of the lab.
the steps are:
1.Declare your variables(there is a list of variables to use).
2.Open each file
3.Use number formatting with each output file
4.Create the input-driven loop
5.Process an input record in the loop body
6.Close the files

And what I have so far based on my notes...

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
ifstream inFile;
ofstream newFile;
int val1, val2, val3;"cisdegree.txt");
if (
cout << "Error opening input file.\n";
exit(1); // 1 indicates an error

inFile >> val1 >> val2 >> val3;
newFile << val1 << " " << val2 << " " << val3 << endl;


return 0;
Use [ code ] ... [ /code ] tags (no spaces around 'code')

1. Rename your variables for the streams -- these are not files, these are streams of the files. Typically inFile would be set to the filename itself, like "cisdegree.txt". This way if there's a a problem opening the file, you can also output the erroneous filename.
2. Make sure your variable types clearly match the file specifications (hint: they shouldnt be all integers), and again you have realllllly bad names for the variables. val1,val2,val3 dont tell you anything about what is stored there.
3. While loop with ifstream object will iterate through each variable, separated by whitespace, and assign everything to those variables. Important to declare them as the correct types. You want as many variables as whitespace-separated things there are in each row of the data file.
string inFile = "cisdegree.txt";
ifstream ifs(inFile);
if (!ifs.is_open())
  cerr << "Error opening file \""<< inFile <<"\".\n";  
  exit(1); // 1 indicates an error

//declare variables you'll need here
while (ifs >> id >> last_name)  // add more as needed, etc.
Last edited on
All shorts of lost.
Is not a question
I know those were not the names, those were just examples from the notes I transferred over. but the variables would be ofstream then the txt majors?
And you're example does not look like anything from the class.

And thank you Samuel for your help.
I'm just overall lost on this assignment.
Last edited on
so you naming the files in the terminal or is it already given to you and and where are your given initials?
in the txt cisdegree it has the student id, name, gpa, and major.
Last edited on
You want as many variables as whitespace-separated things there are in each row of the data file.

Indeed (as implied by the requirement to format output).

However, if the char for major is indeed the last char on line (ignoring the newline), then technically it would be sufficient to grab each line as a whole, decide by the last char, and write out without couple last chars.

Imagine a worker at post office. There is an "incoming" box and several "outgoing" boxes (one for local destinations, one for international, some for other towns).
The worker takes one letter at a time from the incoming, checks the address, and puts it into the appropriate outgoing box. The worker continues as long as there are letters to sort in the incoming box.

Your program is the worker. First establish the boxes. Then handle the data in a loop.
Topic archived. No new replies allowed.