This is my input assigned:
// ID Name Address St Zip Majr Minr Rk QCA AltQCA Crd Hrs
135792468 Wayne,John Duke 101 Hollywood Way CA 40815 CS MATH 10 3.2667 4.0000 49 15

this is my code thus far:

#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <iostream>
#include <cctype>

using namespace std;
 
struct Program
{
	char Number, Name;
	char Code;
	char Major, Minor;
	int Rank;
	char Address;
	};
int i=1,q=0, y=0; 
char t = '\t';
Program ID[9],Student[20],Street[25],Home[2],Zip[5],First[4],Second[4],Status[2];


//function prototypes
void readID(ifstream& In, ofstream& Out, int q);
void readName(ifstream& In, ofstream& Out, int q);
void readRank(ifstream& In, ofstream& Out, int q);
void readMinor(ifstream& In, ofstream& Out, int q);
void readMajor(ifstream& In, ofstream& Out, int q);
void readState(ifstream& In, ofstream& Out, int q);
void readZip(ifstream& In, ofstream& Out, int q);
void readAddress(ifstream& In, ofstream& Out, int q);


void main()
{  	double QCA, AltQCA;
    int QualCred, HrsAtt;
	ifstream In("students1.txt");
	ifstream InData("courses1.txt");
	ofstream Out("qca1.txt");
	Out<<fixed<<showpoint;
	Out<<"QCA Calculation"<<endl;
	Out<<"__________________________________________________________"<<endl;
	Out<<"ID Number    Name      Major     Minor    Rank"<<endl;
	
	In.ignore(255,'\n');
	readID(In,Out,q);
	readName(In,Out,q);
	readMajor(In,Out,q);
   	In>>QCA>>AltQCA;
	In>>QualCred>>HrsAtt;
	
	In.close();
	InData.close();
	Out.close();
}
//functions to read student.dat
void readID(ifstream& In, ofstream& Out, int q){
	In.get(ID[q].Number);
	//reading the ID Number of the student
	for(i=0;i<9;i++)
	{
	Out<<ID[q].Number;
	In.get(ID[q].Number);
	
	}
	Out<<' ';
}

void readName(ifstream& In, ofstream& Out, int q){
	In.get(Student[q].Name);
	for(i=0;i<20;i++)
	{
	Out<<Student[q].Name;
	In.get(Student[q].Name);
	}
	Out<<' ';
}


void readMajor(ifstream& In, ofstream& Out, int q){
	In.get(First[q].Major);
	for(i=0;i<5;i++)
	{
		Out<<First[q].Major;
		In.get(First[q].Major);
	}
}

And my output...
QCA Calculation
__________________________________________________________
ID Number Name Major Minor Rank
135792468 Wayne,John Duke 101 ollyw


so my question is, how do i, when bringing in the input, "skip over" the address, state, and zip code to read and output the major, minor, and rank? Also I need to make sure the remaining information, QCA AltQCA Crd Hrs, are read and stored for future use. Any help is appreciated, thank you!

Recommended Answers

All 6 Replies

You can't just skip over fields. Read the entire line then extract what you want from it and discard the rest.

So, would I have to define a new function? Or do I use a getline??

The first thing you need to do is correct that structure -- you only allowed one character for each of those fields, and of course you can't store a name or street address in a single character. Since this is a c++ program you might want to consider using st::string. If you can't do that then expand the fields to use character arrays, such as char Name[80]; so that you can store up to 80 characters in the name.

As for that line in the file, the best approach may be to read the whole line in memory then parse it apart. And that isn't going to be easy due to the way that line is formatted -- spaces within each of the fields.

It might be easier to parse the line backwards (right to left) instead of the normal way from left to right. We see that major is the 7th field from the end, so start at the end and back up to the 7th word. Each word is separated by a space, so just count spaces (assuming the line has just one space between each word.

there are tabs between each of the categorical data, does that make this any simpler?

Oh, well yes it does. you can use getline() with tab separator, such as getline(infile,name,'\t'); But you still have to fix that structure if you intend to use it for anything.

Oh, well yes it does. you can use getline() with tab separator, such as getline(infile,name,'\t'); But you still have to fix that structure if you intend to use it for anything.

okay, i think i understand how to fix the structure... now, would i use the getline() in each of my function definitions? or would i need to place it in main()?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.