I haven't written basic array code in so long, I have forgotten how to do some of it. I have two data files. There is some data that is common to both, and some that is only in the first and not the second. I am reading this data in and storing it in the form of an array. If a user inputs a value, I am trying to determine if it is in one, both, or neither. This is where I am stuck. Here is what I have:
Sorry it is so long...but I know my problems are in the crazy for/if/for/if loops...but I don't know how to traverse through both arrays without doing this (like I said, it has been a long time!) Any suggestions??
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAX = 100;
void process_data(ifstream &indata, ifstream &indata2, int studentnumber, int snumber[], int studnum[], string sclass[]);
int main()
{
ifstream indata;
ifstream indata2;
int studentnumber, snumber[MAX], studnum[MAX];
string filename, filename2, sclass[MAX];
filename = "student.data";
filename2 = "studentclass.data";
studentnumber = 0;
indata.open(filename.c_str());
if(!indata.is_open()){
cout<<"student.data file cannot be opened."<<endl;}
indata2.open(filename2.c_str());
if(!indata2.is_open()){
cout<<"studentclass.data file cannot be opened."<<endl;}
process_data(indata, indata2, studentnumber, snumber, studnum, sclass);
indata.close();
indata.clear();
indata2.close();
indata2.clear();
}
void process_data(ifstream &indata, ifstream &indata2, int studentnumber, int snumber[], int studnum[], string sclass[]){
int tempsnumber, tempstudnum;
string tempsclass, tempsect, tempgrade, tempslname, tempsfname, tempmajor, tempyear;
int numberstudents = 0;
while(indata){
indata>>tempstudnum;
indata>>tempslname;
indata>>tempsfname;
indata>>tempmajor;
indata>>tempyear;
studnum[numberstudents] = tempstudnum;
numberstudents++;
indata>>tempsnumber;
}
int numberstudentfiles=0;
indata2>>tempsnumber;
while(indata2){
indata2>>tempsclass;
indata2>>tempsect;
indata2>>tempgrade;
snumber[numberstudentfiles] = tempsnumber;
sclass[numberstudentfiles] = tempsclass;
numberstudentfiles++;
indata2>>tempsnumber;
}
cout<<"Student: ";
cin>>studentnumber;
for(int i=0; i<numberstudents; i++){
if(studentnumber == studnum[i] && studentnumber != snumber[i]){
for(int j=0; j<numberstudentfiles; j++){
if(studentnumber != snumber[j]){
cout<<"is not taking any classes"<<endl;
return;
}
}
}
}
for(int i=0; i<numberstudents; i++){
if(studentnumber != studnum[i]){
for(int j=0; j<numberstudentfiles; j++){
if(studentnumber != snumber[j]){
cout<<"is not found in student.data"<<endl;
}
}
}
}
cout<<"is taking: "<<endl;
for(int i=0; i<numberstudentfiles; i++){
if(studentnumber==snumber[i])
cout<<sclass[i]<<endl;
}
return;
}
Comment