Remember my name

Pages: 12
Sorry

 
if(!ifName.is_open())
It's compiling now but it's also completely skipping the 'if's and 'else'. Neither "Welcome back, ." nor "What's your name: "
I didn't put any of my code in main. If you didn't, then you need to change that.
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
#include <iostream>
#include <fstream>

using namespace std;

#define NEWLINE '\n'
#include <windows.h>  // needed for Sleep(millisec)

int main ()
{
  system("color 02");
  string response;
  string name;
  ifstream ifName;
  ofstream noName;
  ifName.open("Names.txt");

if(!ifName.is_open())
{
  if (!ifName.eof())
  {
    getline(ifName, name);
    cout << "Welcome back, " << name << endl;
    ifName.close();
  }
  else
  {
    ifName.close();
    noName.open("Names.txt");
    cout << "Please enter your name: ";
    getline (cin, name);
    noName.close();
  }
}
cout << NEWLINE;
	cout << "What can I do for you? " << endl;
	getline (cin, response);
    Sleep(2000);

  return 0;
}


I believe that I do. I have to leave for a couple hours but I'll look over it again when I get back. I believe everything should be right. Can I get your opinion?
I played with your code. It frustrated me, but it works now.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define NEWLINE '\n'
#include <windows.h>  // needed for Sleep(millisec)

int main ()
{
  system("color 02");
  string response;
  string name;
  ifstream ifName;
  ofstream noName;
  ifName.open("Names.txt");

if(ifName.is_open())
{
	ifName >> name;
  if (!ifName.eof())
  {
    ifName >> name;
    cout << "Welcome back, " << name << endl;
    ifName.close();
  }
  else if(ifName.eof())
  {
    ifName.close();
	noName.open("Names.txt", ios::app);
    cout << "Please enter your name: ";
    getline (cin, name);
	noName << name << endl;
    noName.close();
  }
	cout << NEWLINE;
	cout << "What can I do for you? " << endl;
	getline (cin, response);
}
else
	cout << "Cannot open file" << endl;
    Sleep(2000);

  return 0;
}
This is really frustrating. I copied and pasted your code into mine and I had to change a couple minor things and then it worked. It worked exactly how I wanted it to. I tried copying the slightly altered code from my test file to my main file but now neither want to work.. God, I hate this thing.

The "What can I do for you? " is suppose to be outside all the brackets. Plus, if I straight copy and paste your code now and into a brand new file, it only says Cannot open file.
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
int main()
{
  system("color 02");
  string response;
  string goodbye;
  string reply;
  string name;
  ifstream ifName;
  ofstream noName;
  ifName.open("NAMES.txt");

cout << "Please use proper capitalization. Thank You!" << endl;

if(ifName.is_open())
{
	ifName >> name;
  if (!ifName.eof())
  {
    ifName >> name;
    cout << "Welcome back, " << name << endl;
    ifName.close();
  }
  else if(ifName.eof())
  {
    ifName.close();
	noName.open("NAMES.txt", ios::app);
    cout << "Please enter your name: ";
    getline (cin, name);
	noName << name << endl;
    noName.close();
  }
  else
	cout << "Cannot open file." << endl;
}
  do {
cout << NEWLINE;
loop:
    cout << "What can I do for you? Type NOTHING to continue. ";
    cin >> reply;
cout << NEWLINE;


What in the world could I have messed up? I put the 'else' into the brackets the first time and it worked after I did that. Now it wont.
Last edited on
Stick with my code. If you put what can I do for you outside the brackets, then it will always run regardless of whether or not the file opens and you'll never know when it doesn't. As for the file, you need to create a text file with notepad and save it as Names.txt

ifstream won't create a file that doesn't exist.
Last edited on
But I need "What can I do for you" to always run.
It going to be the main piece of my puzzle.

ifstream doesn't create a file like fstream?
So that was the problem. I erased the old file "Names" and changed it to "Namefile" within the code but I assumed that it would be automatically created.
=P Thank you!!! =D =D =D =D
Do you know how I'd be able to update my program where it'd effect its "copies", like I'd like to send it to a friend so he can check it out?
So if I add/change my code, I don't have to resend my program to him again and again?
I don't think so. My guess is that that would be included in networking.
I've looked up on google about updating your c++ program and its said some things about Visual 2010 so i downloaded it but i guess some things r different on visual than codeblocks.. Thank you so much.
My next task is to loop it back to the "Please enter your name: " if the user inputs "erase/reset name"
and then being able to access My Documents and the like with user inputs.

fun stuff
frustrating as heck though
If you want something interesting like that, you could create a text file that maintains all document names and you could access it that way. I might just do something like this now. If you really want to have some fun, work on making it into a user/password program with individual account text files.
dam, you're getting way ahead of me; for now. only started learning last friday. but i will keep that in mind.
or is there a way that I can search all my files based on a search within my program, like u would do using 'Run'?
Probably. I just think it would be easy to use a simple file. If I could figure out how to make the file so it is unable to be edited, then I would be on my way. All I can do with files for now is read and write. If I could get a GUI to interact with the file, then I'd have it made.
dude... i lost my xbox live mic haha
Yeah, you would. Dam, there would be so much to do then
Topic archived. No new replies allowed.
Pages: 12