File I/O help.

I want my program to be able to write to a txt file with the information that the user inputs. Then i want it to be able to reopen the file, and read from that same file to get the account names and passwords to be able to "log" on. And also, does it have to be in a loop?
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

	
	

int main() 
{
	ifstream inFile;  //input file
	ofstream outFile; //output file
	int name;
	int password;
	int Exit = 0;
	const int SENTINEL = Exit;
	char userSelect;

	
	cout <<"*******Final Project*******\n\n\n"<< endl;
	
		


	cout << "\n Please select from the three options.\n"
		 << "B to Begin\n"
         << "C to Create\n"
         << "E to Exit"
         << endl;
 
	 cin >> userSelect;
	
switch (userSelect)
	{
default:
	cout << " That is an invalid option." <<endl;
case 'C': case 'c':
	inFile.open("C:\accountname + pw.txt");

inFile >> name;
    cout << "Choose a Name you would like to use and type exit to quit.\n";
	 cin >> name;
	cout << " " <<endl;
	cout << "\n" <<endl;

inFile >> password;
	cout << "choose your password.";
	 cin >> password;
	cout << "\nChose your Race."<<endl;

	cout << "!Dwarf! !Orc! !Minotuar! !Human! !Elf!"<<endl;
inFile.close();
break;
	
case 'B': case 'b':
	
	cout << "Type in your user name\n."
		 << "Type in your password.\n"
		 << endl;
break;

case 'E': case 'e':
	cin >> exit;
}
	

return 0;
}
Hi Epoch.
hmm ok lets start with your variables....
You want the user to input some text but you are using an int variable to store it.
You should use a c string char *name, *password; or the string class <string> i recommend string class:
http://www.cplusplus.com/reference/string/
1
2
string name;
string password;

You are also missing some break;'s in your loop fix that and some logical errors like
1
2
3
4
5
inFile >> name;
    cout << "Choose a Name you would like to use and type exit to quit.\n";
    cin >> name;
    cout << " " <<endl;
    cout << "\n" <<endl;


also:
 
inFile.open("C:\accountname + pw.txt");


if you want a '\' in your string you have to add 2 like
inFile.open("C:\\accountname + pw.txt");

i'm guessing the file name here is wrong too;
you might want something like this:
1
2
3
4
5
string fileName, accountName, fileExt;
fileExt ="pw.txt";
cin << accountName;
fileName = accountName + fileExt;
inFile.open(fileName.c_str());

take a look at the open mode's for the open function:
http://www.cplusplus.com/reference/iostream/fstream/open.html

consider checking if the file is actually open before using it:
1
2
3
4
5
6
7
8
9
10
if(inFile.is_open())
{
//use your file and then close it
//...
inFile.close();
}
else
{
     cout << "error opening the file\n";
}

And some other errors, fix as many errors as you can and come back your new code and some compile errors(if any).

J
Topic archived. No new replies allowed.