Yes. The best way would be to save the user name in the text file, as James2250, mentioned, then, when you re-start the program, and load the file in, ask if this is 'Bob' or whatever name was entered and saved. If the user answers yes, continue with the program, cout << "Welcome back, " << etc., otherwise, ask for the users name, save, then continue.
Would it be possible to have it be some kind of if function? "If" it doesn't find an inputted name at the start of a program, it asks for a name but "if" it does find the inputted name, it doesn't ask the question but preforms a cout, in my case.
int main()
{
// Looks for a saved name.
if (saved name is not found)
cout << "Please enter your name. " << endl;
else (saved name is found)
cout << "Welcome back, " << name << "."
...continues!
Yea, store it in a file. If only one name is stored in the file, you just check for eof when opening. If you open and eof is hit immediately, then no name is in there. If you hit some chars first, grab them and output them
int main()
{
// *Program starts*
// Opens file
// Reads file and for the eof
if (saved name is not found)
cout << "Please enter your name. " << endl;
else (saved name is found)
// Extract data from file and import it back into the code
cout << "Welcome back, " << name << "."
// Continue..
So, is this the exact process that I need to do? Or am I missing a step or two??
I should probably take a break with trying to write my program and get a better understanding of all the terminology. haha
Well, eof = end of file. You don't really have to do a separate test for it. Have you looked at fstream at all? It has all the answers you need for this solution. A program does not remember anything about it's previous run without some outside assistance
You should look into this like James says. The fstream file allows you to input and output data to and from files. This is a really good tutorial and has helped me a lot.
I know have this but an error is showing up: 'else' without a previous 'if'
I have it where the file opens, if the file isn't at eof it gets the "name" and inputs it into the cout and then the file closes.
I've got it to the point of starting but all it would do is show "Welcome back, ." and then *click any key to continue*
Resident, if I kept it as (!fstream.eof()), it gives me an error: expected primary-expression before '.'
Thanks GRex, now I'm running into an if/else issue. When I compile, it reads the "Welcome back, John." as I want it to but it also completes the else and reads "Please enter your name: ".
So, I guess I'll be spending another hour or more trying to find out how to fix this without causing more errors. lol google search here I come, again..
If anyone has any input that'll help and save me maybe a couple hours, that'd be AMAZING.
edit: When I delete anything within my Names.txt file, it still displays the "Welcome back, ." but without a name. But with it displaying the What's your name part, is because it sees that it has data, it inputs it and then it sees that it is at eof so it still displays the Else. grr means even more work
You need to put your else statement in brackets and make sure it's running with the if as the first option. Maybe take out the blank line between the if and the else, but I don't think that's it.
You have to open a file (namefile.txt) write the name or names onto it. Save it. This you do if the file is empty or you need a new name in the file. When your program start it read the file and check for the names. If the name exist in the file simply print the the name. "Hello Bob. Welcome back".
Look up fopen(), fclose and other related read and write files on google.
Following program will look a file named "name.txt". If the file don't exist it will create it and ask for name. Next time program starts up it looks for the file a print out the name in the file.
I think that was what you were looking for.
int main ()
{
{
if ()
{
//the Welcome back stuff
}
else
{
//the What's your name stuff
}
}
//more stuff
return 0;
}
I moved the 'if' outside the first inner start bracket as you mentioned which got the program to compile.
I have the same thing with the 'else', outside the second inner start bracket.
Both the 'if' and 'else' are within the outer bracket. (Not the main brackets.)
Just to clarify, I have it where it will now read the name from the Names.txt file and input it.
If there is a name in the file, it inputs it like it should but if there is no name, it still displays:
"Welcome back, ." but with no name.