reading from a .txt

I am trying to read from 3 separate lines in a text file but it won't assign the each line to string1, string2, string3.

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
  #include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

string setlevel1();
string setlevel2();
string setlevel3();

bool uselevel1(string password);
//bool uselevel2(string sentence);
//bool uselevel3(string question);

int main()
{
	ifstream fin;
	ofstream fout;
	fin.open("instorage.txt");
	fout.open("instorage.txt", ios::out | ios::trunc);
	//make a set password and test passwords function
	char choice;
	cout << "[1]Set passwords or [2]test security?" << endl;
	cin >> choice;
	if (choice == '1')
	{
		cout << endl;
		string str1 = setlevel1();
		cout << endl;
		string str2 = setlevel2();
		cout << endl;
		string str3 = setlevel3();
		cout << endl;
		fout << str1 << endl;
		fout << str2 <<endl;
		fout << str3 << endl;
	}
	else if (choice == '2')
	{
		string string1;
		string string2;
		string string3;
		getline(fin, string1);
		getline(fin, string2);
		getline(fin, string3);
		uselevel1(string1);
	}
	else
	{
		cout << "ERROR: YOU DID NOT ENTER A CHOICE" << endl;
		system("pause");
		exit(0);
	}
	fin.close();
	fout.close();
	system("pause");
	return 0;
}
What gets read into string1, string2, string3 ?
What does setlevel1() setlevel2() setlevel3() return ?
Your code has linker errors, you have no definition of uselevel or setlevel1, 2, or 3 shown.
string1 is the only important variable because I can't get it too read from the .txt file I have. Once I get string1 to read a line and store it too string1 then I will do the same with string2 and string3
setlevel1(), setlevel2(), and setlevel3() all return strings that are stored in str1, str2, and str3. These are the lines that i'm trying to store in string1,2,3.
I also have definitions for uselevel(),setlevel1,2,3. I just chose to not include them because they are irrelevant to what I am trying to fix
Instead of just doing
getline(fin, string1)

try wrapping it in an if statement to check if the getline was successful.

1
2
3
4
if (!getline(fin, string1))
{
    std::cout << "Error: Unable to process file" << std::endl;
}

thank you but that didn't help, I got the ERROR statement that the if statement produces. I don't think it's even executing the getline().
Last edited on
Here is my whole code, if you can figure this out please tell me!!
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

string setlevel1();
string setlevel2();
string setlevel3();

bool uselevel1(string password);
//bool uselevel2(string sentence);
//bool uselevel3(string question);

int main()
{
	ifstream fin;
	ofstream fout;
	fin.open("instorage.txt");
	fout.open("instorage.txt", ios::out | ios::trunc);
	//make a set password and test passwords function
	char choice;
	cout << "[1]Set passwords or [2]test security?" << endl;
	cin >> choice;
	if (choice == '1')
	{
		cout << endl;
		string str1 = setlevel1();
		cout << endl;
		string str2 = setlevel2();
		cout << endl;
		string str3 = setlevel3();
		cout << endl;
		fout << str1 << endl;
		fout << str2 <<endl;
		fout << str3 << endl;
	}
	else if (choice == '2')
	{
		string string1;
		string string2;
		string string3;
		fin >> string1;
		uselevel1(string1);
	}
	else
	{
		cout << "ERROR: YOU DID NOT ENTER A CHOICE" << endl;
		system("pause");
		exit(0);
	}
	fin.close();
	fout.close();
	system("pause");
	return 0;
}


//start level 1 code
string setlevel1()
{
	string password;
	bool one = false, two = false, three = false, four = false;
	cout << "First level of security program." << endl;
	cout << "Enter a password" << endl;
	cout << "(it must contain 1 upper case and 1 lower case letter ";
	cout << "at least 6 characters long and one digit)" << endl;
	cin >> password;
	if (password.length() < 6)
	{
		one = false;
	}
	else
	{
		one = true;
	}
	for (unsigned int x = 0; x <= password.length(); x++)
	{
		if (isdigit(password[x]))
		{
			two = true;
		}
		else if (isupper(password[x]))
		{
			three = true;
		}
		else if (islower(password[x]))
		{
			four = true;
		}
		if (one == true && two == true && three == true && four == true)
		{
			cout << "PASSWORD IS VALID" << endl;
			return password;
		}
	}
	cout << "PASSWORD IS INVALID" << endl;
	system("pause");
	exit(0);
}
//end level 1 code
//start level 2 code
string setlevel2()
{
	string sentence;
	bool one = false, two = false;
	cout << "Second level of security program" << endl;
	cout << "Enter a sentence you would like to use ";
	cout << "(it must contain more than one word and end in a '.')" << endl;
	cin.clear();
	cin.ignore();
	getline(cin, sentence);
	for (unsigned int x = 0; x <= sentence.length(); x++)
	{
		if (sentence[x] == ' ')
		{
			one = true;
		}
		else if (sentence[x] == '.')
		{
			two = true;
		}
	}
	if (one == true && two == true)
	{
		cout << "PASSWORD IS VALID" << endl;
		return sentence;
	}
	else
	{
		cout << "PASSWORD IS INVALID" << endl;
		system("pause");
		exit(0);
	}
}
//end level 2 code
//start level 3 code
string setlevel3()
{
	string question;
	cout << "This is level 3 of the security program" << endl;
	cout << "You will be given a security question to set" << endl;
	cout<<"Were did you go to high school?" <<endl;
	cin.clear();
	getline(cin, question);
	cout << "PASSWORD IS ACCEPTED" << endl;
	return question;
}
//end level 3 code

//start of using level 1 code
bool uselevel1(string password)
{
	string word;
	for (int x = 0; x < 5; x++)
	{
		cout << "You have "<<5-x<<" attempt(s) to enter the right password" << endl;
		cout << "Password: ";
		cin >> word;
		if (word == password)
		{
			cout << "CORRECT" << endl;
			return true;
		}
		else
		{
			cout << "INCORRECT" << endl;
		}
	}
	return false;
}
//end of using level 1 code 
Whenever I use fin>>string1 or getline(fin, string1) and watch the variable using vs it says string1 = ""
Sounds like your file is not being opened correctly.

You should always check to make sure your file is opened

1
2
3
4
5
6
fin.open("instorage.txt");

if (!fin)
{
    std::cout << "Error: Unable to open file" << std::endl;
}


Most likely, you aren't putting your file in the right directory. Try putting the file either in the same directory as your project file (if you're using an IDE), or in the same directory as your executable file (.exe if windows).
Topic archived. No new replies allowed.