C++ Problem Encountered

Hello,

Below is my code for Reading, Finding, and Converting strings from Text file, which is a project to my class in C++.

The project is divided into 3 major parts.

1. Read text from file (ifstream).
2. Remove comma from text file that separates the records (strtok).
3. Search Record from text file. (strcmp).

My project is running with no errors, as you can check the code below. The only thing that my professor found that little mistake, is that when I entered a non-existing ID number, the program "must" tell something like "Record NOT found." I don't know where to add it. Thank you for those who can give me the location where to put the cout << "Record Not FOUND"; line. :)

FYI: I use devC++ UI; C++ code are written by me, thanks to cplusplus.com References. :)

Use this dummy text below, paste in notepad or other text editor and save it as "sample.txt"

001,John,Doe
002,Lady,Doll
003,Princess,Star
004,Allen,Smith


I really need your help. Thank you very much in advance. :)

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
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    
    string buffer;
    char id[100];
    string fname,lname;
    int find=0;

    start:
    cout << "Find ID: ";
    cin >> id;          //accepts user input for ID

    ifstream data("sample.txt");
    
    if(!data.is_open())
    {
     cout << "File is already opened.";                  
    }
    else
    {
        while(!data.eof())
        {
           getline(data, buffer);
           char *newstr = new char [buffer.length()+1];

           strcpy(newstr, buffer.c_str());
           
           char *ch = strtok(newstr, ",");  

           int x=1;
           while(ch!=0)
           {
                find=strcmp(newstr, id);
                
                if(find==0)
                {
                    if(x==1) cout << "ID Number: ";
                    else if(x==2) cout << "First Name: ";
                    else if(x==3) cout << "Last Name: ";               
                    
                    cout << ch << endl;

                    x++;                    
   
                } //<-------closing find IF
                    ch = strtok(NULL, ",");
                       //if(data.eof()) cout << "Record Not found."; <<< currently not working
           } //<-------- closing ch WHILE
        } //<--------closing data.eof WHILE
           data.close();
           cout << endl;
           goto start;                    
    }
        system("PAUSE");
        return EXIT_SUCCESS;

 }
from line 53 if(data.eof()) cout << "Record Not found.";

NOTE: I have placed it anywhere but with no luck. I've searched the cplusplus forum and references but couldn't point me where to put that line to make the program tells "Record Not Found".
First, if you are going to use the std::string class, you should #include the <string> header. Also, why do you need the char arrays when using std::string? Or strtok / strcmp, for that matter? There is always std::string::find() / std::string::operator== for your use.

Anyway, as to your question, how about you put a boolean value to check to see if the record is found in your program? That means that once you have found the record you want you don't need to parse the rest of your file, and if the end of the file is reached without you setting that value to true you can output that error message.
That's great! It helped a lot NT3. Thank you!

this helped me.. Thanks for your hint or tip or whatever you called that.. :)

as to the string header, it was not included while copying the source code. My bad.

I use bool checker and that worked perfectly fine.

As to your suggestion about std::string::find, Thanks mate. You enlightened me what class to use.

Thank you very much. :)
Just sharing to all people out there :D

this one really helped my problem!!

1
2
searchresult=true;
                 break;



And found this too.. now I'm not going to use char arrays instead std::string.. thank you mate :)
if(empcode[loopx].find(searchcode)!=string::npos || fname[loopx].find(searchcode)!=string::npos)
Topic archived. No new replies allowed.