How to insert data to textfile with spaces

Hello guys,

Now i am trying to insert a data to my text file but my problem is how to include the spacebars. My program will ask me what is the name..whenever i input a full name. My program will only recognize my 1st word.. hmm how to work with this problem?
There is a problem when i change to getline( , )
Error: No matching func to call getline..

By the way sir, here is my current code.

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

int main()
{
FILE *fp;
char *ptrname;
char *ptrseat;
int index;
char name[100];
char seat[20];
int selection;
ptrname = name;
ptrseat = seat;
char name1[100];
int lines_read = 0;
string line_of_file;
string nameline;

cout<<"Good Day Sir/Maam!\n";
cout<<"What is your First name?\n";
cin.getline(name,100);
system("cls");
cout<<"What do you want me to do?\n";
while (selection != 5)
{
cout<<"\n1. Check all user registered with their seats\n2. Check if my name is registered\n3. Register someone\n4. Check Occupied Seats\n5. Exit\n";
cin>>selection;
system("cls");

switch (selection)
{
case 1:

cout<<"\nThe files from your text are: \n";
{
string line;

fstream myFile("Sample.txt");
if(myFile.is_open())
{
while (! myFile.eof())
{
getline (myFile,line);
cout<<line<<endl;

}
cin.get();
myFile.close();
system("pause");
system("cls");
}

else cout<<"Unable to open file";
}

break;
case 2:
cout<<"Well, let us see if you are registered here. . .\n";
{
ifstream input_file("sample.txt");

getline(input_file, line_of_file);
if(input_file)
{

if (line_of_file == name)
{

cout << "Seat number -> "<< line_of_file<<endl;
cout << "So you are already registered "<<name<<"\n";

}
else
cout<<"You are not registered\n";

}
else
break;

}
system("pause");
system("cls");
break;
case 3:

{
cout<<"What is the name? \n";
cin>>name1;
cin.get();
cout<<"Welcome Davao";
cout<<"Where do you want to seat? (<digits 1-8><letters a-z> sample 4B)\n";
cin>>seat;
cin.get();
ofstream outfile;
outfile.open("sample.txt", ios::out|ios::app);

outfile<<name1<<endl;
outfile<<seat<<endl;

system("pause");
system("cls");
outfile.close();

}
break;



case 4:
{
cout<<"Here are the list of the occupied seats\n";
ifstream input_file("sample.txt");
while ( !input_file.eof() )
{
getline(input_file, line_of_file);
if(input_file)
{
++lines_read;
if(lines_read % 2 == 0 )
cout << "Seat number -> "<< line_of_file<<endl;
}
else break;
}
}
system("pause");
system("cls");
break;

case 5:
cout<<"Bye";
cin.get();
break;

default:
cout<<"Invalid";
system("cls");

break;
}
}
return 0;
}




I'll be waiting for someone who will help me.. :)
Last edited on
I think you need to use the getline() function differently:

For example, if you want to store something in a string variable called s you would type:

1
2
3
4
5
string s;
cout << "All the words entered will be stored in the string until when you hit return";
getline(cin, s);
// You can change the character that delimits the end of the line by adding another parameter to
// getline(). for example getline(cin, s, '/t') and you can replace /t with whatever you need 


Also you if you are going to mix cin with getline statements you might run into other problems, like inputs that appear to be arbitrarily skipped.
Look up the ignore() function if that happens


Hi good evening sir,

I am encountering the same problem.

Error: No matching func to call getline..

How to fix this error? hmm
the definition of the function getline() is included in string.
In order to use it you have to add this line to your program

 
#include<string> 


also make sure you are including iostream as well
i already include the

#include <string>

sir.. above is my current code.. :)
I the code you posted compiles fine on g++ 4.3, which compiler are you using?
I think im using MingW..

hmm
Are you using Dev-C++?
Yes i am sir..:)
That's your problem right there. The version of MinGW bundled with Dev-C++ is very old. If you want that to compile, you'll have to use something different. Code::Blocks is not a bad choice.
Thank you for the comment sir.. i think i should change my compiler now.. hmm.. i think i will try to change from MinGW to g++.. what do you think? :)
MinGW and g++ are the same things on Windows, Try to get a newer version of it
A few definitions:
GCC: the GNU Compiler Collection (previously, the GNU C Compiler). A group of compilers developed by the GNU project.
g++: one of the compilers in GCC. Other compilers include gcc (capitalization is important) and g77.
MinGW: Minimalistic GNU for Windows. A port of GCC to Windows.
Topic archived. No new replies allowed.