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?
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
usingnamespace 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";
}
elsebreak;
}
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;
}
elsebreak;
}
}
system("pause");
system("cls");
break;
case 5:
cout<<"Bye";
cin.get();
break;
default:
cout<<"Invalid";
system("cls");
break;
}
}
return 0;
}
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
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.
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.