Hello everyone!
I am a computer science student taking my first C++ class and this is my first time posting a query online. So, I apologize in advance for my lack of savvy. Up until now, I have not had any problems whatsoever but my current assignment is throwing me for one heck of a loop so I was hoping one of you more experienced folk can point me in the right direction.
These are my assignment instructions:
1 - Your assignment is to prompt the user to enter the filename with the path on disk. If the file does not exist in the specified location, your program should exit with a suitable error message.
2 - The first thing your program should do is output to the screen a copy of the data read in from the disk file. This is known as “echoing” the input data.
3 - Your program should then calculate and display the average score for males, females, community college students, and university students. Display your results to two places of decimals, and write your program to automatically list your four calculated averages one to a line, along with a suitable label for each result.
4 - Finally, your program should calculate and display to two places of decimals the overall average score for all of the students surveyed.
Questions:
1 - I understand loops fairly well but I don't know how to get the while loop to read info from a data file, one "column" at a time. They are not allowing us to do arrays yet but even if they did, I think arrays work line by line instead of column by column.
I have a feeling though, that through the use of loops, I can potentially manipulate the data search to the point that it will bypass everything except the data I need but I have no idea if that is the direction I need to go or if it is a matter of figuring out how to get a loop to read data from specific columns.
2 - The provided text file is this:
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
Peterson F UN 69
Hsu M UN 79
Bowles M CC 75
Anderson F UN 64
Nguyen F CC 68
Sharp F CC 75
Jones M UN 75
McMillan F UN 80
Gabriel F UN 62
Since I am not outputting data to the file but instead, querying for input from the file, I would guess that I do not need any outfile commands but I am not sure about that. I am guessing the teacher put a list of names in there to make it harder to access the data I need (like the gender column). I am happy to provide more details if needed and thank you for your time! Here is a short template of my approach, it is just the gender scores so far:
I am a computer science student taking my first C++ class and this is my first time posting a query online. So, I apologize in advance for my lack of savvy. Up until now, I have not had any problems whatsoever but my current assignment is throwing me for one heck of a loop so I was hoping one of you more experienced folk can point me in the right direction.
These are my assignment instructions:
1 - Your assignment is to prompt the user to enter the filename with the path on disk. If the file does not exist in the specified location, your program should exit with a suitable error message.
2 - The first thing your program should do is output to the screen a copy of the data read in from the disk file. This is known as “echoing” the input data.
3 - Your program should then calculate and display the average score for males, females, community college students, and university students. Display your results to two places of decimals, and write your program to automatically list your four calculated averages one to a line, along with a suitable label for each result.
4 - Finally, your program should calculate and display to two places of decimals the overall average score for all of the students surveyed.
Questions:
1 - I understand loops fairly well but I don't know how to get the while loop to read info from a data file, one "column" at a time. They are not allowing us to do arrays yet but even if they did, I think arrays work line by line instead of column by column.
I have a feeling though, that through the use of loops, I can potentially manipulate the data search to the point that it will bypass everything except the data I need but I have no idea if that is the direction I need to go or if it is a matter of figuring out how to get a loop to read data from specific columns.
2 - The provided text file is this:
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
Peterson F UN 69
Hsu M UN 79
Bowles M CC 75
Anderson F UN 64
Nguyen F CC 68
Sharp F CC 75
Jones M UN 75
McMillan F UN 80
Gabriel F UN 62
Since I am not outputting data to the file but instead, querying for input from the file, I would guess that I do not need any outfile commands but I am not sure about that. I am guessing the teacher put a list of names in there to make it harder to access the data I need (like the gender column). I am happy to provide more details if needed and thank you for your time! Here is a short template of my approach, it is just the gender scores so far:
Code:
#include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; int main() { //Declare variables to manipulate data char sex; string name; string school; string fileSource; int maleScore; int femScore; int UniScore; int commcollScore; int maleTotal; int femTotal; int uniTotal; int commcollTotal; double sum = 0; int femCount = 0; int maleCount = 0; int score; //Declare stream variables ifstream inData; ofstream outData; inData >> name >> sex >> school >> score; // Promt user for file location cout << "Please input file location: "; cin >> fileSource; // open output file and run program exit failsafe command inData.open(fileSource); if (!inData) { cout << "Cannot open input file. " << "Program will now terminate." << endl; return 1; } outData << fixed << showpoint << setprecision(2); // echo the data file while (inData) { cout << name << sex << school << score << endl; inData >> name >> sex >> school >> score; } // while reading incoming data from file, execute the conditions while (inData) { if(sex=='M') { maleScore = maleScore + maleTotal; ++maleCount; } else if(sex =='F') { femScore = femTotal; ++femCount; } } }
Comment