Hello Kat21909,
Here are some tips to help you:
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
Your use of blank lines is a good start, but you have to many where you do not need them and none where you do. As an example the struct would look better as:
1 2 3 4 5 6 7 8 9 10
|
struct student
{
string Fname;
string Lname;
string ID;
string Email;
string Exam1;
string Exam2;
string Exam3;
}s;
| |
And in "main" these lines would look better as:
1 2 3 4 5 6 7 8
|
fstream inFile("inData.csv");
ofstream outFile("outData.csv");
if(!inFile.is_open())
// ------------------------------------
fstream inFile("inData.csv");
ofstream outFile("outData.csv");
if(!inFile.is_open())
| |
It just breaks thing up into parts that are more readable.
Regular variable names generally start with a lower case letter and camelCase is most often used in names. So your "Fname" would be "fName".
The line
if(!inFile.is_open())
can be shortened to just
if(!inFile)
. It does the same thing and you might consider the code:
1 2 3 4 5 6 7 8 9 10
|
std::string inFileName{ "" }; // <--- Put full file name here.
std::ifstream inFile("inFileName");
if (!inFile)
{
std::cout << "\n File " << inFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5)); // <--- Needs header files chrono" and "thread".
return 1; //exit(1); // Use the "exit" if not in "main", i.e., another file.
}
| |
Line 8 is optional. I use this to keep the console window open long enough to read the message before the return closes the window. You may not need this line. The return will exit the program because there is no reason to continue until you fix the problem. Otherwise you would not be able to read anything from the file because the stream is not connected to the file. The only part to deal with is the number in the parentheses which is the whole number of seconds it will wait until the program continues.
The same concept can be used for the output file stream.
The first while loop condition based on checking for "eof" will not work the way you expect. Checking for "eof" in the while condition usually means that you will process the last read twice. This is most likely why you have the line
if (inFile.eof() ) break;
, but it is in the wrong place to be useful. it should be after the first getline which is the first read of the file that could set "eof".
This is most often used to read a file of unknown length:
1 2 3 4 5 6 7 8
|
while (getline(inFile, Fname, ','))
{
getline(inFile, Lname, ',');
getline(inFile, ID, ',');
getline(inFile, Email, ',');
getline(inFile, Exam1, ',');
getline(inFile, Exam2, ',');
getline(inFile, Exam3, '\n');
| |
This way when the getline fails to read anything "eof" is set and the while condition becomes false, so the while loop fails and the next line after the while loop is executed.
Another thing you could do is read directly into the struct then push it into the vector. This would eliminate seven lines of variables and seven lines of code following your getlines(). For the lines that start "outFile" use the struct variables before you read the next record.
Your menu is OK, but if something would go wrong, say entering a letter either by choice or by accident there is no way to deal with the problem. Trying to enter a letter into a numeric variable will cause "cin" to fail and become unusable until it is reset. When it comes to a menu I like to put this in a function and use a do/while loop to keep the menu going checking the input and only returning a valid choice.
The last thing I see is that you are trying to close your files inside the switch. Because of the break statements these lines will never be reached. They should be done outside the switch and outside the final while loop.
All that said are you asking if the program follows the instructions? For now I thin your code covers most of the instructions. I will know more once I get the program loaded and compiled.
One last note: your program uses an input file. Posting the input file or at least a useful sample of the file is most helpful. This way everyone will be using the same information.
Hope that helps,
Andy