Can someone tell how to read every second line of this code.. and for the search

Here is my code pal

#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];


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

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();

cin.get();

cout<<"\nPress Enter\n";
system("pause");
system("cls");
}

else cout<<"Unable to open file";
cin.get();
}

break;
case 2:
cout<<"Check taken seats";
cin.get();
break;
case 3:

{
cout<<"What is the name? \n";
cin>>name1;
cin.get();
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;

cout<<"\nPress any key to exit\n";

cin.get();
outfile.close();

}
break;

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

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

break;
}
}
return 0;
}




How to read every second line of my code in the textfile?
Read every line as you are, but just throw away (ignore) every other line you read.
how to throw every odd line i read sir? hmm thanks for the reply
Consider this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream input_file( "foo.txt" );
int           lines_read = 0;
string     line_of_file;

while( !input_file.eof() ) {
    getline( input_file, line_of_file );
    if( input_file ) {
        ++lines_read;
        if( lines_read % 2 == 0 )
            cout << "Read the line: " << line_of_file << endl;
    }
    else break;
}

Thanks pal. now i got it now.. hehe Now i can move to the next problem which is to search in my text file.. can someone help me again? hehe ^__^

I am glad that jsmith gives me the solution for my first problem
Thanks pal. now i got it now.. hehe Now i can move to the next problem which is to search in my text file.. can someone help me again? hehe ^__^

I am glad that jsmith gives me the solution for my first problem
Search for what?
Sorry i didn't specify...

Just like if the user will search for the name...
There is a response that the name he/she search for is an existing name in the textfile. :)

Thank you for the reply sir.. I am so noob. sorry

by the way here is my code..


#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;
}




The case : 2 will check if your name is already registered in the text file.. My problem is .. am i on the right track? .. hehe because there is a problem in searching a string in my text file.

Another problem sir is how i can manage to return the name of the passenger and his seat in CASE 2


Thank you for trying sir.. :)
Last edited on
Topic archived. No new replies allowed.