Can someone teach me how to search from textfile..

i am stuck in searching words from textfile..
Can you be more specific?
http://cplusplus.com/forum/articles/1295/
here is my code.. THE EDITED VERSION

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




i get an error when i try to search for a vacant seat..


How to go there?


Enter your name (only 1 word)
Then press 5
Then try to search for a seat..
If the seat is unavailable.. an error will exist
Last edited on
Next time use [code][/code] tags please
You are mixing C and C++, which language are you supposed to use? I'd suggest you using C++ streams instead of C handles
im sorry bazzy.. i am a noob guy.. next time..

Thank you sir.. at least for this day i learned something from you..

can you help me? :(
next time..


Doesn't have to be next time, you can edit your post right now. (see the edit button?) Just select the code and click the little hash button on the side.

can you help me? :(


He is:
I'd suggest you using C++ streams instead of C handles


Take a look at cin and fstream:

http://www.cplusplus.com/doc/tutorial/basic_io/ (scroll down some and you'll find cin)
http://www.cplusplus.com/doc/tutorial/files/ (all about files and fstream)

After reading those, you should be able to replace all your C with C++, which is good for you, since it will be easier to work with. printf() can be replaced with cout and scanf() can be replaced with cin. When you switch from C file handles to C++ streams, you should also be able to change your char*'s to strings, again making it much easier to work with.
Just finished editing my codes.. please help.. :O
Topic archived. No new replies allowed.