#include <iostream>#include <fstream>usingnamespace std;
int gtut (int, int);//protoype gradint utut (int, int);//prototype undergrad tuitionint main()
{
ifstream input;
ofstream results;
input.open("/Users/putyoursoxon/Documents/input.txt");//input file
results.open("/Users/putyoursoxon/Documents/results.txt");//application results go hereint loopnum, i, idnum, state, credits, gd, datasum, dataavg, j;
char study;
gd = 1;
int idnuma[gd];
int statea[gd];
int creditsa[gd];
int rega[gd];
char studya[gd];
input >> loopnum;//number of students to be evaled
datasum = 0;
for (i = 0; i < loopnum; i ++)
{
input >> idnum >> state >> study >> credits;
if (study != 'u' && study != 'g' || state != 0 && state != 1 || credits <= 0 || idnum < 1 || idnum > 999)//error checking
{
results << "There is an error in the input data for student " << idnum << endl;
if (study != 'u' && study != 'g')
{
results << "The character " << study << " is not a valid input for the Status of Study. Please input a u for undergrad or a g for graduate student" << endl;
results << endl;
}
if (state != 0 && state != 1)
{
results << "The Character " << state << " is not a valid input for the Status of Residence. Please input a 0 for in-state or a 1 for out-of-state" << endl;
results << endl;
}
if (credits <= 0)
{
results << credits << " is not a valid credit load" << endl;
results << endl;
}
if (idnum < 0 || idnum > 999)
{
results << idnum << " is not a valid Student id number" << endl;
results << endl;
}
}
else
{
if (studya[gd-1] == 'u')
rega[gd-1] = utut(creditsa[gd-1], statea[gd-1]);//undergrad tuition calledif (studya[gd-1] == 'g')
rega[gd-1] = gtut(creditsa[gd-1], statea[gd-1]);//grad tuition called
datasum = datasum + rega[gd-1];
gd ++;
}
}
results << "Student ID# Residence Study Credit# Fee" << endl;
results <<"------------ --------- ----- ------- ---" << endl;
for (j = 0; j< gd; j++)
{
results << idnuma[j] << " ";
if (statea[j] == 1)
results << "Out-of-state ";
if (statea[j] == 0)
results << "In-state ";
if (studya[j] == 'u')
results << "Undergraduate ";
if (studya[j] == 'g')
results << "Graduate ";
results << creditsa[j] <<" ";
results << rega[j] << endl;
}
dataavg = datasum / gd;
results <<"The average registration fee is: $" << dataavg << endl;
input.close();
results.close();
return 0;
}
int gtut (int fcred, int fstate)//grad tuition
{
int freg;
freg = 0;
if (fcred <= 9)
freg = freg + (fcred * 400);
if (fcred > 9)
freg = freg + 3600;//grad pays max 9 creds for tuitonif (fstate == 0)
freg = (freg * 0.35);
if (fcred >= 12)//general fees
freg = freg + 516;
if (fcred < 12)
freg = freg + (fcred * 43);
freg = freg + 10;//activities feereturn freg;
}
int utut (int fcred, int fstate)//undergrad tuition
{
int freg;
freg = 0;
freg = freg + (fcred * 280);
if (fstate == 0)
freg = (freg * 0.35);
if (fcred >= 12)//general fees
freg = freg + 516;
if (fcred < 12)
freg = freg + (fcred * 43);
return freg;
}
and here is the input file
12 1234 1 u 13 234 2 g 12 119 0 f 20 560 0 g -17 450 0 g 20 111 0 g 6 999 1 g 14 767 1 g 6 345 0 u 20 498 0 u 8 970 1 u 18 383 1 u 8
and the results file
There is an error in the input data for student 1234 1234 is not a valid Student id number
There is an error in the input data for student 234 The Character 2 is not a valid input for the Status of Residence. Please input a 0 for in-state or a 1 for out-of-state
There is an error in the input data for student 119 The character f is not a valid input for the Status of Study. Please input a u for undergrad or a g for graduate student
There is an error in the input data for student 560 -17 is not a valid credit load
else
{
idnum = idnuma[gd-1];
state = statea[gd-1];
study = studya[gd-1];
if (studya[gd-1] == 'u')
rega[gd-1] = utut(creditsa[gd-1], statea[gd-1]);//undergrad tuition calledif (studya[gd-1] == 'g')
rega[gd-1] = gtut(creditsa[gd-1], statea[gd-1]);//grad tuition called
datasum = datasum + rega[gd-1];
gd ++;
but Im still getting these jumbled results. Cant figure out what is wrong
There is an error in the input data for student 1234 1234 is not a valid Student id number
There is an error in the input data for student 234 The Character 2 is not a valid input for the Status of Residence. Please input a 0 for in-state or a 1 for out-of-state
There is an error in the input data for student 119 The character f is not a valid input for the Status of Study. Please input a u for undergrad or a g for graduate student
There is an error in the input data for student 560 -17 is not a valid credit load
in lines 18 - 23 you initialize gd, and your arrays, however, i could not find anywhere in your code where you used dynamic memory allocation to increase your array size. thus:
1 2 3 4 5 6
gd = 1;
int idnuma[gd];
int statea[gd];
int creditsa[gd];
int rega[gd];
char studya[gd];
initializes each array to only have a size of one... meaning it's not really an array, it's just one value. so on line 66, when you increment gd, you are actually effectively going past the end of the array. anything past the end of the array is not guaranteed to be valid, and overwriting data past the end of an array can cause the program to crash. So those weird values you are getting are most likely due to accessing memory blocks past the end of the array (which c will allow you to do) that do not contain any relevant data. Hopefully this helped!
input >> loopnum;//number of students to be evaledint idnuma[loopnum];
int statea[loopnum];
int creditsa[loopnum];
int rega[loopnum];
char studya[loopnum];
and put this later on this gd int to 0
1 2 3 4 5 6 7 8 9 10 11 12
else
{
idnum = idnuma[gd];
state = statea[gd];
study = studya[gd];
if (studya[gd] == 'u')
rega[gd] = utut(creditsa[gd], statea[gd]);//undergrad tuition calledif (studya[gd] == 'g')
rega[gd] = gtut(creditsa[gd], statea[gd]);//grad tuition called
datasum = datasum + rega[gd];
gd ++;
but my results file is still f'ed up, now I'm lost, can anybody help?
if I want to use a vector, how exactly word I set it up, is it like a array, I havent learned about vectors yet, I tried to use the reference, but im a little confused
ok i figured it out, idiot mistake, i was doing idnum = idnuma[gd] instead of the correct idnuma[gd] = idnum etc etc
another question I have though is if I want to sort thru the list I now have a array that is (loopnum) long but only has data for the amount of (gd), so there is empty space in the array, can I still search it? Like if I wanted to list people who had under twelve credits, wouldnt the empty array spaces mess up the results??
If you have data in those spots, it could screw it up. That's why a vector would be a better idea...anyway, you can look here for info on how to use vectors: