Returning string value

Hello, I need help with this little program I am making for the excersise,
I am trying to check if account.txt file exist in my project folder, using FindingAccount function when if I does return the name of that project(in this case account.txt), and use that name to open account info, and if it does not exist reapeat the input while using recursion.

Problem is that string acc gets return value of nothing , and then error appears because it cannot open the 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
 // main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "account.h"

std::string FindingAccount(std::string & x);

int main()
{
	std::cout << "please enter account number to see its account info" << std::endl;
	std::string accountNumber3, acc;
	acc = FindingAccount(accountNumber3);
	std::cout <<"Returned x: " << acc << std::endl;

	std::ifstream is(acc);
	std::string line;
	std::string name, surename;
	int accountNumber,age, id, digit4Passm;
	float accountBalance;
	int lineCount = 0;

	if (is.is_open())
	{
		std::cout << "account.txt is open" << std::endl;
		while (std::getline(is, line)) 
		{
			if (lineCount == 0)
			{
				accountNumber = std::stoi(line);
				lineCount++;
				std::cout <<"line 1 : " << line << std::endl;
			}
			else if (lineCount == 1)
			{
				name = line;
				std::cout << "Line 2: " << line << std::endl;
				lineCount++;

			}
			else if (lineCount == 2)
			{
				surename = line;
				std::cout << "Line 2: " << line << std::endl;
				lineCount++;
			}
			else if (lineCount == 3)
			{
				age = std::stoi(line);
				lineCount++;
			}
			else if (lineCount == 4)
			{
				id = std::stoi(line);
				lineCount++;
			}
			else if (lineCount == 5)
			{
				digit4Passm = std::stoi(line);
				lineCount++;
			}
			else if (lineCount == 6)
			{
				accountBalance = std::stof(line);
				lineCount++;
			}
		}

	}


	Account acc1(accountNumber, name, surename, age, id, digit4Passm, accountBalance);
	std::cout << acc1.getAge() << std::endl;


	std::cout << "Do you wnt to change information of the account? Y/N" << std::endl;
	char c;
	std::cin >> c;
	
	switch(c)
	{
	case'Y':
	case'y':
		std::cout << "We will change information for account" << std::endl;
		break;
	case'N':
	case'n':
		std::cout << "We will not change information for account" << std::endl;
		break;
	default:
		break;
	}

	system("pause");
	return 0;
}


std::string FindingAccount(std::string & x)
{
	std::string name = x;
	if (std::ifstream(x))
	{
		std::cout << "This account exists" << std::endl;
		std::cout << "returning x : " << x << std::endl;
		return x;
	}
	else
	{
		std::string y;
		std::cin >> y;
		FindingAccount(y);
	}
	std::cout << "returning name : " << x << std::endl;
	return x; 
}

//account.h--------------
#pragma once
#include <iostream>
#include <string>
class Account
{
private:
	std::string name,surename;
	int accountNumber,age, id, digit4Pass;
	float accountBalance;
public:
	Account();
	Account(int accountNumber , std::string Name, std::string Surname, int age, int id, int digit4Passm, float accountBalace);
	
	//setters
	void setName(std::string x)
	{
		name = x;
	}
	void setSurname(std::string x)
	{
		surename = x;
	}
	void setAge(int x)
	{
		age = x;
	}
	void setID(int x)
	{
		id = x;
	}
	void setPassword(int x)
	{
		digit4Pass = x;
	}
	void setBalance(float x)
	{
		accountBalance = x;
	}
	//Getters------
	std::string getName()
	{
		return name;
	}
	std::string getSurename()
	{
		return surename;
	}
	int getAge()
	{
		return age;
	}


};
Account::Account(int accountNumber2,std::string Name, std::string Surname, int age2, int id2, int digit4Passm, float accountBalaceNew)
{
	accountNumber = accountNumber2;
	name = Name;
	surename = Surname;
	age = age2;
	id = id2;
	digit4Pass = digit4Passm;
	accountBalance = accountBalaceNew;
	std::cout << "Constructor is 1 working" << std::endl;

}
Account::Account()
{
	std::cout << "Constructor is  2 working" << std::endl;
}

//account.txt-------------
1
name
surename
18
1234
1234
100.00;
Last edited on
If you are using a recursive function, you should make it tail recursive if possible (i.e., the recursive call should be the last operation before the return). And you don't need to pass a string in.

1
2
3
4
5
6
std::string FindAccount()
{
    std::string name;
    std::getline(std::cin, name);
    return std::ifstream(name) ? name : FindAccount();
}

I can't say I like it, though. I'd use a regular loop, more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    std::ifstream is;
    std::string acc;
    while (true) {
        std::cout << "Enter account number to see its account info\n";
        std::getline(std::cin, acc);
        if (acc.empty()) { // empty line exits program
            std::cout << "Exiting...\n";
            std::exit(0);
        }
        is.open(acc);
        if (is)
            break;
        std::cerr << "Can't find account file " << acc << '\n';
    }

And you could read your data like this if you want:

 
is >> accountNumber >> name >> surname >> age >> id >> digit4Passm >> accountBalance;

Thanks, it helped a alot
Topic archived. No new replies allowed.