Whitespace question

I've been working on a banking system where you have a few options of what you can do. I'm not that far in, but I'm in need of help when it comes to whitespaces. When pressing option 2 for an existing account it works only partly. What I mean is that When I enter in an existing account with only the first name it will say "Account Found" (which is what I want). However when I enter in both the first and last name it can't read it because of the spacing in the text document called "Accounts.txt". For example When entering in John only it works, but entering in John Doe it doesn't. I've looked around and saw some examples,but can't find the explanation behind how it worked. This is a new concept to me and wish for a simple explanation behind it and how I should alter my current coding.

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void display(char& anwser);
void N_account(char& anwser,string & name);
void Exist(string& name_search, char& anwser, string name_from_file);


int main()
{
	int start_money;
	string name, name_search, name_from_file;
	char anwser;
	display(anwser);
	N_account(anwser,name);
	Exist(name_search, anwser,name_from_file);
}

void display(char& anwser)
{
	cout << setw(65) << "=================" << endl;
	cout << setw(65) << "Banking Managment" << endl;
	cout << setw(65) << "=================" << endl;
	cout << setw(60) << "1.New account" << endl;
	cout << setw(65) << "2.Existing account" << endl;
	cout << setw(56) << "3.Deposit" << endl;
	cout << setw(57) << "4.Withdraw" << endl;
	cout << setw(62) << "5.Close account" << endl;
	cin >> anwser;
}

void N_account(char& anwser,string & name)
{
	if (anwser == '1')
	{
			ofstream outfile;
			outfile.open("Accounts.txt",std::ofstream::out|std::ofstream::app);
			cout << "Enter in first and last name for new account:";
			cin.ignore();
			getline(cin, name);
			outfile << name;
			outfile << '\n';
			cout << "Account added" << endl;
			outfile.close();

	}	
}

void Exist(string & name_search,char & anwser,string name_from_file)
{
	if (anwser == '2')
	{
		ifstream infile;
		infile.open("Accounts.txt");
		cout << "Enter in your account:";
		cin.ignore();
		getline(cin, name_search);
		while (infile >> name_from_file)
		{
			if (name_from_file == name_search)
			{
				cout << "Account found: " << name_search << endl;
				
			}
		}
		infile.close();
		
	}
}
Last edited on
A sample extract from your file would be helpful to us for determining what is going wrong. Without the file any advice would be useless.

I can make a conjecture where at least one of the problems lie: line 61. Using operator>> for reading a file has the same problems when extracting from std::cin if you want a string that might contain one or more spaces.
Last edited on
Here you go. These are existing accounts.

1
2
3
4
5
John Doe
Jane Doe
Travis Scott
William Smith
Patrick Michaels
Line 61: while (std::getline(infile, name_from_file)), same as you do at lines 43 & 60.
Last edited on
Thank you my man! simpler than I though it would be.
A stream is a stream, whether it is std::cin or a file. If you need std::getline() to properly read console input, you'll likely need it for a file when reading similarly formatted data.
Topic archived. No new replies allowed.