Hello! I have a project that is due tonight and I am at the last step.. However, everything I tried has not worked so far.
What am I trying to Achieve?
Well, I have two .txt files.. namely candidates.txt and merit.txt which are manipulated through other functions. And what I am trying to do is read merit line by line, storing every word in each structure string, compare it's candidate's respective attributes and store it in a new file depending on the type of merit list generated which is also compared with both files.
menu_generate()
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
start_generate:
system("cls");
struct name
{
string namef;
string namel;
};
struct infof
{
int marks;
string category;
string group;
};
struct form
{
int formno;
int session;
name personal;
infof data;
};
struct grade
{
int om;
int army;
int def;
};
struct infom
{
int listno;
grade quota;
string group;
};
struct list
{
int session;
infom data;
};
struct infoi
{
int listno;
string quota;
string group;
};
struct input
{
int session;
infoi category;
};
form student;
list merit;
input generate;
string col[2], in, record;
char cmd;
system("cls");
cout<<"\n\t\t Generate"<<endl;
cout<<"_________________________________________"<<endl;
cout<<"Back\t\t\t\t\tX\n"<<endl;
cout<<"\n Hint: The Capital Letters Represents\n\tthe Input Commands."<<endl;
cout<<"\n\tSession: ";
getline(cin,in);
if(in=="b")
{
system("cls");
return(0);
}
if(in=="x")
{
system("cls");
exit(0);
}
generate.session=stoi(in);
if(!((generate.session>=0)&&(generate.session<=9999)))
{
cout<<"\t ERROR! Session out of bound.";
getch();
system("cls");
goto start_generate;
}
cout<<"\tMerit List: ";
getline(cin,in);
if(in=="b")
{
system("cls");
return(0);
}
if(in=="x")
{
system("cls");
exit(0);
}
generate.category.listno=stoi(in);
if(!((generate.category.listno>=1)&&(generate.category.listno<=3)))
{
cout<<"\t ERROR! MeritList out of bound.";
getch();
system("cls");
goto start_generate;
}
cout<<"\n\t Quota: ";
getline(cin,in);
if(in=="b")
{
system("cls");
return(0);
}
if(in=="x")
{
system("cls");
exit(0);
}
generate.category.quota=in;
if(!((generate.category.quota=="civ")||(generate.category.quota=="om")||(generate.category.quota=="army")||(generate.category.quota=="def")||(generate.category.quota=="gov")))
{
cout<<"\t ERROR! Quota out of bound.";
getch();
system("cls");
goto start_generate;
}
cout<<"\t Group: ";
getline(cin,in);
if(in=="b")
{
system("cls");
return(0);
}
if(in=="x")
{
system("cls");
exit(0);
}
generate.category.group=in;
if(!((generate.category.group=="cs")||(generate.category.group=="econ")||(generate.category.group=="stats")||(generate.category.group=="engg")||(generate.category.group=="med")))
{
cout<<"\t ERROR! Group out of bound.";
getch();
system("cls");
goto start_generate;
}
cout<<"\n\t Are you Sure? (y/n): ";
in=getche();
if(!(in=="y"))
goto start_generate;
else
{
ifstream fetch_candidate("candidates.txt",ios::in);
if(!fetch_candidate)
{
cout<<"\tERROR! \'candidates.txt\' Not Found."<<endl;
getch();
system("cls");
return(0);
}
else
{
while(fetch_candidate>>student.formno)
{
getline(fetch_candidate>>ws,student.personal.namef,'\t');
getline(fetch_candidate>>ws,student.personal.namel,'\t');
fetch_candidate>>student.data.category
>>student.data.group
>>student.data.marks
>>student.session;
ifstream fetch_merit("merit.txt",ios::in);
if(!fetch_merit)
{
cout<<"\n\t ERROR! \'merit.txt\' Not Found.";
getch();
system("cls");
return(0);
}
else
{
while(fetch_merit>>merit.session
>>merit.data.listno
>>merit.data.group
>>merit.data.quota.om
>>merit.data.quota.army
>>merit.data.quota.def)
{
if((generate.session==merit.session)&&(generate.session==student.session)&&(student.session==merit.session))
if(generate.category.listno==merit.data.listno)
if((generate.category.group==merit.data.group)&&(generate.category.group==student.data.group)&&(student.data.group==merit.data.group))
{
if((((generate.category.quota=="civ")||(generate.category.quota=="om"))&&(student.data.category=="civ"))&&(student.data.marks>=merit.data.quota.om))
{
ofstream list_om("LISTOM.txt",ios::app);
list_om<<student.formno<<'\t'
<<student.personal.namef<<'\t'
<<student.personal.namel<<'\t'
<<student.data.marks<<'\t'
<<student.data.group<<'\t'
<<student.data.category<<'\t'
<<student.session<<'\n';
}
if(((generate.category.quota=="def")||(generate.category.quota=="gov")&&(student.data.category=="def")||(student.data.category=="gov"))&&(student.data.marks>=merit.data.quota.def))
{
ofstream list_def("LISTDefGov.txt",ios::app);
list_def<<student.formno<<'\t'
<<student.personal.namef<<'\t'
<<student.personal.namel<<'\t'
<<student.data.marks<<'\t'
<<student.data.group<<'\t'
<<student.data.category<<'\t'
<<student.session<<'\n';
}
if(((generate.category.quota=="army")&&(student.data.category=="army"))&&(student.data.marks>=merit.data.quota.army))
{
ofstream list_army("LISTArmy.txt",ios::app);
list_army<<student.formno<<'\t'
<<student.personal.namef<<'\t'
<<student.personal.namel<<'\t'
<<student.data.marks<<'\t'
<<student.data.group<<'\t'
<<student.data.category<<'\t'
<<student.session<<'\n';
}
}
}
}
}
}
}
cout<<"\n\tMerit List Successfully Generated!";
getch();
goto start_generate;
}
But, I have no idea why... The function takes input and as soon as it moves on list generation, it exits. by exit, i mean the program ends even though i put a confirmation and a getch(); command. P.s; Classes&Vectors are NOT Allowed.
Yeah, i'm basically tired now.. That's why i didn't try more than just that in the end where List Generation (File Creation & Data Append) works.
The project is due by midnight and my group members didn't even write a single word of code. So, they're useless and i've been staying up until 7 Am and waking at 1 Pm daily working on this project alone and my mind is fed up..
Maybe i should read candidate.txt first and then read merit? i'm not sure anymore... Please help. :(
EDIT Another theory of the error cause is that the ofstreams to create and append a new list is failing to do so... But still, why would it skip getch(); and confirmation cout afterwards? it should execute those before exiting.. and even before that, it should return to the previous function Home and not just straight-up exit. :/
You need to post the full text of the assignment. Your description doesn't give enough information about what you're trying to do. Remember, we aren't in your class. We don't even know what school you're going to. We have no idea what the assignment is.
Consider it from my perspective:
Well, I have two .txt files.. namely candidates.txt and merit.txt
So far, so good!
which are manipulated through other functions.
Wait. Manipulated how? Does that mean you start with some manipulated data? Now the starting conditions are unclear.
And what I am trying to do is read merit line by line
Okay, so I guess the whole "manipulated by other functions" thing doesn't matter?
storing every word in each structure string
Merit.txt contains mostly numbers, not words. So are you only storing the words? What happens to the number? And what are these "structure strings" your talking about?
compare it's candidate's respective attributes
Hmm. So a merit has a candidate? Which merit's correspond to which candidates? And "respective attributes?" Does that mean merits are related to candidate attributes? How? None of the column headers in the candidates file talks about attributes. In fact, none of the column headers in candidates matches any of the column headings in merits. So if there's a relationship between them, it's not apparent.
and store it in a new file depending on the type of merit list generated
So... you generate a merit list somehow? And that list has a type? Is that "econ", "stats", "med", etc? Then why is the heading "OM"?
which is also compared with both files.
So the output is compared with the input files? or something else? What's compared? How is it compared? What's the purpose of the comparison?
One of the functions simply adds a new line of merit in the same format as the file.. and doesn't delete or show anything from the file.
The other, add a single line at a time that includes the data of a candidate into the candidates.txt file
Take it as one function is trying to transfer data to another function through a file and then compares them.. whichever lines have similar words in the file, it simply appends it into a new file.
By structure string i meant the structure of strings above.. which, is surprising long. xD
It simply makes it easier to identify what value the variable holds by it's name.
And by Attributes, i mean.. in the last step of list generation, the program needs to see if the merit requested to generate exists in both the files.. it checks both files line by line, for example.. if i input value of generate.session = 2019, generate.listno = 1 and group = "cs"(all the previous values will check which line of merit.txt matches these values and stores them in merit.session, merit.listno, etc..)
while quota = om means it does not care if the candidate.category is civ, gov, army, or def. it simply appends it into a new file if the candidate.session, candidate.group match..
But the next ofstreams also check if candidate.category is the same as required list to be generated such as army and only appends the data of the candidates if their candidate.category=="army"
That is how it compares the values of both files.
Also, i added the error, do you have any idea what it's about?
Sorry, i updated the file and forgot to update the file sample here..
But yes, there is session column in candidate.txt
line 184 means 'as long as it can read a line' ???? I'm not sure, but that's what our teacher told us. however, it worked when i printed the entire candidate.txt file in another funtion?
yes, 186-191 is my mistake, it should be fetch_merit and not fetch_candidate.
What i'm trying to do is;
1. take user input of what session, listno, group and quota does the user want to generate the merit list of? all these values are stored in generate structure.
2.1 then the program needs to read the header/column titles of the file into col[1] //this is just to eliminate the header in the calculation.
2.2 it is supposed to read the first line and store all of the words into separate variables within merit structure.
3.1 it again needs to read the first header/column title into col[2] just to eliminate it.
3.2 after that, now comes the tricky part.. It needs to read each line of the candidate.txt file and store it into candidate structure.
4.1 and everytime it reads one line, it should compare if merit.session==candidate.session if yes, then it needs to check if merit.group==candidate.group.
once these two are out of the way, it checks who the candidate belongs to, armed forces, civilian, gov employee, etc... regardless of these, the ofstream list_om (stands for open merit) should ignore the category and append the data of the candidate into a new file.
4.2 whereas the upcoming ofstream list_army checks if candidate.category=="army" and only appends those who have it..
4.3 same with ofstream list_def, the only difference is that it checks candidate.category=="gov"||"def"
Note that all ofstreams need to create different files and append data int their respective ones..
Maybe one of the files isn't opening and you don't see the error message because you're clearing the screen. You've forgotten to put getch() calls before those system("cls") calls.
I don't like PMs. And I'm going to be busy anyway.
But I notice that you aren't reading from the merit.txt file properly.
Get rid of the while (fetch_merit >> record) part (which makes no sense)
and replace it with
(Obviously you would get rid of the similar code below that, too.)
You may also have your goto in the wrong place at the end, so it may only read the first record.
I added more conditions in the final If statements before ofstreams...
There is only one tiny problem now, it somehow, however.. always gives 1 List of Def/Gov Category regardless if i don't even ask it to do that. :/
I could put all final conditions into their else statements..?
EDIT Nah it's still doing that even after putting them into their else statements.