Hi,
I am trying to write a program where the user inputs strings (name, last name, phone number), and save these data into a notepad.
The user should be able to decide if he/she wants to continue with new data to the notepad file created (loop). If the file already has any content, it may not be overwritten, new personal data should only be added.
I have written the following code, but I am not sure why, once I open the notepad, the info are not on the same line.
For the second part, I am not sure how to begin.
Hints would be appreciated.
When you direct endl to the outfile, it writes a line-ending. That's why everything you output is on a new line. If you don't want a new line, don't write endl.
notepad is just a text viewing program, one of thousands. You are trying to work with a text file, not a notepad file, just for clarity.
endl works the same in files as it does on the console. The endls are why its not on the same line in the file. Replace endl with " " or something and only write one endl at the last part of the text.
for the append, open the file as append and its fine.
where you have file.open, you want
file.open(name, ios::app); to append to it.
I am still having issues with this part of the task:
The user should be able to decide if he/she wants to continue with new data to the notepad file created (loop). If the file already has any content, it may not be overwritten, new personal data should only be added.
The options suggested above do not work, and anytime I rerun the program, the data in the txt file gets erased and replaced by the new input. This should not happen. It should only be added as already specified above.
Anyone?
char cont{};
do
{
// lines 20 - 34
std::cout << "\nDo another y/n? ";
std::cin >> cont;
} while (std::toupper(cont)=='Y');
Line 31 would work better as a "std::getline" in case there is a space in the phone number.
And line 34 would work better as: std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. as there may be more than one character to ignore and the default of "cin.ignore()" is one character or a newline whichever comes first.