Hello Silver29,
If you are still interested and catch this. Some suggestions on what you did;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream> // For the common codes. For input and output (cin) and (cout)
#include <iomanip> // setw(), fixed, setprecision, std::quoted().
#include <limits> // <--- Added.
#include <string> // For the string function/container. For the std::string class and its member functions.
#include <fstream> // For the ofstream function which basically allows you to create or open a file. For file stream input and output.
using namespace std; //This will allow you to not use 'std::' in every line (almost). Best not to use.
// <--- Puts "std::" infront ov everything to see if it is in the standard name space. Can cause problems.
int main() //Dunno what this is for, but you need to put this every part
{
string fullname;
std::cout << "Name: "; //a text to show the user that he needs to input his name, this will also be his file name
getline(cin, fullname); //will scan the whole name input... then save it to filename container which will then become the file name
//ofstream outFile; //This is your container for the file you want to export
//outFile.open("C:\\Users\\USER\\Documents\\" + fullname + ".txt"); //This code will open or create the file
std::string outFileName{ "C:\\Users\\USER\\Documents\\" + fullname + ".txt" }; // <--- Put File name here.
std::ofstream outFile(outFileName);
if (!outFile)
{
return std::cerr << "\n File " << std::quoted(outFileName) << " did not open\n", 1; // <--- Requires header file "<iomanip>".
//return std::cerr << "\n File \"" << outFileName << "\" did not open.\n", 1;
}
//if (outFile.is_open()) //An if-else statement that will see if the file opened or not
//{
int age{}; //container for age, 'int' for integer. ALWAYS initialize all your variables.
string CurrentSchool, Section;// <--- Strings are empty when defined and do not need initialized.
cout << "Age: "; //cout for age. Prompt for age.
cin >> age; //age container
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
if (age > 17) //check if younger than 18
{
cout << "Section: ";
getline(cin, Section);
cout << "Current School: ";
getline(cin, CurrentSchool);
cout << "\n\nThank You for filling out this form.\n\n\n";
}
else
{
//everything here is same as the above, only different data and under else due to the age,
//if user is under 18, then user can't continue
cerr << "\n You're too young.\n\n";
}
outFile
<< fullname << '\n'
<< age << '\n'
<< CurrentSchool << '\n'
<< Section << '\n';
outFile.close();
//}
//else
// cout << "\nERROR!\n File failed to be created, please check permission(s) and try again.\n\n\n"; //Error Text
return 0; // <--- Not required, but makes a good break point for testing.
}
| |
Some of your comments I have added to. Others I I will explain.
int main() //Dunno what this is for, but you need to put this every part
|
This is the starting or entry point of the program. Every program must contain 1 and only 1 "int main()". And you never call "main" from inside a program or from a function.
Since "fullName" is part of the file name you should define that string first then deal with the "ofstream" before you go any farther. Since you have a path to go with the file name this is 1 part that can cause an "ofstream" to fail if the path is wrong.
Most of the time an output file does not use a path which allowes the file to be created in the current working directory of the program. If you are not sure what the current working directory is this can help you find it, but most the current working directory is the same place that the ".cpp" is stored.
By checking the file streams first, meaning , input and/or output, streams, you exit the program to fix the problem rather than continuing with the program because there is no point in continuing if something did not open.
This negates the need for the if/else statement that you have. Yes it is OK and it does work, but to me it is kind of tacky and does not look very good.
In lines 58 - 62 you do not need a "cout" or "outFile" and "endl" for each line. the insertion operator (<<) can chain everything together into just 1 statement. And the compiler does not care about white space when it reads your source code, so make it easy to read.
BTW for lines 26 and 27 if you do not want to include the "<iomanip>" header file just switch the comments.
You should notice that I used "std::cerr" here. This still outputs to the screen, but as I recently learned it also flushes the buffer before whatever comes next.
It is similar to a "cout" followed by a "cin". The "cin" will flush the buffer before any input is taken.
Andy