Remember my name

Pages: 12
I've been trying to figure out how to get my program to ask for your name once and never have to add it again.

*Program starts for 1st time*
What's your name: Bob
*name "Bob" is stored*

*Program starts again*
Hello Bob.

How to get it to remember your name. Once I can get it to do that, I'll worry about trying to add a "Change Name" feature or a *Reset* or some sort.

First things first. Get it to ask for and store data on the initial start up and then not ask for it again, but to remember your name.
I suggest you store the name inside a textfile when the user enters it, then it can read from that file when the program is ran again.

If you aren't sure how to do this you should look into fstream.
http://www.cplusplus.com/reference/iostream/fstream/
Would it be possible to make it only ask for their name or any other data on the initial start up and never ask again unless requested by user?
@TruYadoo

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.
1
2
3
4
5
6
7
8
9
10
11
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!


This is kinda something that I'd like it to do.
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
Last edited on
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
James2250 wrote:
If you aren't sure how to do this you should look into fstream.
http://www.cplusplus.com/reference/iostream/fstream/


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.
Try this
1
2
3
while(!fstream.eof()){
//perform stuff here
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  system("color 02");
  string response;
  string name;
  fstream myfile;
  myfile.open ("Names.txt");

    {
    if (!myfile.eof())
      getline (myfile,name);
      cout << "Welcome back, " << name << "." << endl;
      myfile.close();
    }

    else
	  cout << "Please enter your name: ";
	  getline (cin, name);
	  cout << "Welcome, " << name << "." << endl;
cout << NEWLINE;

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 '.'
Last edited on
1
2
3
4
5
6
    {
    if (!myfile.eof())
      getline (myfile,name);
      cout << "Welcome back, " << name << "." << endl;
      myfile.close();
    }


If is inside the brackets.

1
2
3
4
5
6
    if (!myfile.eof())
    {
      getline (myfile,name);
      cout << "Welcome back, " << name << "." << endl;
      myfile.close();
    }


This should work. It was probably a typo.
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char OutputFileName[50];
FILE *fileptr;

void OpenFile(void)
{
    sprintf(OutputFileName, "name.txt");

    if ((fileptr = fopen(OutputFileName, "w+t")) == NULL) {
        cprintf("ERROR IN OPENING FILE: %s. ESC to quit", OutputFileName);
    }
}

void CloseFile(void)
{
    fclose(fileptr);
}
Last edited on
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.

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
71
72
73
74
75
76
77
78
79
80
#include <conio.h>
#include <stdio.h>
#include <string.h>

#define CR 13

FILE *fileptr;

char OpenFile_W(void);
char OpenFile_R(void);
void CloseFile(void);
void ReadString(void);

char szBuffer[100];

/*----------------------------------------------------------------------*/

char OpenFile_W(void)
{
    if ((fileptr = fopen("name.txt", "a+")) == NULL) {
        return -1;
    }
    return 0;
}

char OpenFile_R(void)
{
    if ((fileptr = fopen("name.txt", "r")) == NULL) {
        return -1;
    }
    return 0;
}


void CloseFile(void)
{
    fclose(fileptr);
}

void ReadString(void)
{

char cCh;
int  iPtr;

    iPtr = 0;
    while ((cCh = getch()) != CR)
    {
        putch(cCh);
        szBuffer[iPtr++] = cCh;
    }

   // End string with NULL
    szBuffer[iPtr] = 0;
}

/*----------------------------------------------------------------------*/

int main()
{

    if (OpenFile_R() != 0) {

        cprintf("Type your name: ");
        ReadString();
        cprintf("\r\nYour name = %s\r\n", szBuffer);
        OpenFile_W();
        fprintf(fileptr, "%s", szBuffer);
    }

    fgets(szBuffer, 100, fileptr);
    printf("Hello again %s\r\n", szBuffer);

   // Wait for keypress
    getch();
    CloseFile();

    return 0;
}
 
@ GRex

I have it as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.

the 'if' works, the 'else' isn't.


Last edited on
Or you could do this very simply. Create an ifstream variable and an ofstream variable. I'm pretty sure that you can't use a simple fstream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ifstream ifName;
ofstream noName;

ifName.open("name.txt")
if(ifname.is_open)
{
  if(!ifName.eof())
  {
    getline(ifName, name);
    cout << "Welcome back " << name << endl;
    ifName.close();
  }
  else
  {
    ifName.close();
    noName.open("name.txt");
    cout << "Enter name:  ";
    getline(cin, name);
    noName.close();
  }
}
else
  cout << "Cannot open file" << endl;
error: Could not convert 'ifName.std::basic_ifstream<_CharT, Traits>::is_open....
Pages: 12