I have a problem with my console program.

I have a text file named "myRecord.txt" with the following contents:

101,Carlito Caballero,500.00
102,Tina Gamboa,560.34
103,Angel Caballero,750.55
104,Kyla Shane,850.32
105,Jogie Glen Mait,1000.00

If I run the program, the console should prompt the user to enter the id number (which is the set of numbers on each beginning of a line),

ID:___

and the output would be a profile of the id number entered.

//prompts user to enter a specific id number
ID:101

//output
ID:101
Name:Carlito Caballero
Rate:500.00

I have tried to tokenize the whole file but a getline cant have delimiters in it. I also had tried to find something on the text file but it only finds a string entered and not the whole line.

Any help would be appreciated.

Recommended Answers

All 7 Replies

>>I have tried to tokenize the whole file
Post what you have tried.


>>getline cant have delimiters in it.

std::string word;
ifstream in("filename.txt");
while( getline(in,word,',') )
{
  do something with this token
}

This is what i had tried to do:

I tried to find the id number but it just give the correct id number but not the name and the rate:

void find()
{
    char size=100;
    char string[size],temp[size][32];
    char * tokenPtr;
    int i=0;

    cout<<"Enter ID number to search: ";
    cin.getline(string,size);

    tokenPtr =strtok(string," ,.");

    while (tokenPtr != NULL)
   	{
      	strcpy(temp[i],tokenPtr);
         i++;
      	tokenPtr = strtok(NULL," ,.");
    }
   	for(int j=0;j<i;j++)
    {
   	 char word1[300],word2[300];

        ifstream jogieglen("myRecord.txt");
        ifstream janmarion("myRecord.txt");

         while (jogieglen>>word1)
         	if (strcmp(word1,temp[j])==0)
   				 tokenPtr = strtok (word1," ,.-");
   				 cout<<"ID: "<<temp[j]<<endl;
   				 while (janmarion>>word2)
         	if (strcmp(word2,temp[j])==0)
   				 tokenPtr = strtok (word2," ,.-");
   				 cout<<"Name: "<<temp[j]<<endl;
    }
}

The Name: on the last part is just my poor attempt to at least show something.

But thanks to the idea, can i add other punctuations on the delimiters?

I imagine you and the OP from this thread 2 days ago are in the same class :P

http://www.daniweb.com/forums/thread312919.html

Your delims don't need to include the 'dot' as you can read in a number in ascii and convert it to a float with atof() or to an int with atoi().

Actually, yeah. He's my colleague. You're the same person who gave him the code. It was correct. Thanks for your help. Im looking forward to work with you again. Thanks Colezy!

Hey, thanks for you help. I think you should change your status from still learning to adept. Thanks a lot!

actually i had made a code for myself using the ideas given to me on previous posts

void find()
{

    ifstream data("myRecord.txt");
    string item,line;
    int x=0;
    int y=0;
    string id;

    cout<<"Enter Employee Id: ";
    cin>>id;

    while(!data.eof())
    {
        getline(data,line);
        string item_array[10];
        stringstream stream(line);
        x=0,y=0;

        while(getline(stream,item,','))
        {
            item_array[x]=item;
            x++;
            item_array[x]=item;
            y++;
        }

        if(item_array[0]==id)
        {
            cout<<"***********************************"
                <<"*****************"<<endl;
            cout<<setfill('.')<<left<<setw(35)<<"ID: "
                <<right<<" "<<item_array[0]<<endl;
            cout<<setfill('.')<<left<<setw(35)<<"Name: "
                <<right<<" "<<item_array[1]<<endl;
            cout<<setfill('.')<<left<<setw(35)<<"Rate: "
                <<right<<" "<<item_array[2]<<endl;
        }
    }

    data.close();
}

it works.

actually i had made a code for myself using the ideas given to me on previous posts

void find()
{

    ifstream data("myRecord.txt");
    string item,line;
    int x=0;
    int y=0;
    string id;

    cout<<"Enter Employee Id: ";
    cin>>id;

    while(!data.eof())
    {
        getline(data,line);
        string item_array[10];
        stringstream stream(line);
        x=0,y=0;

        while(getline(stream,item,','))
        {
            item_array[x]=item;
            x++;
            item_array[x]=item;
            y++;
        }

        if(item_array[0]==id)
        {
            cout<<"***********************************"
                <<"*****************"<<endl;
            cout<<setfill('.')<<left<<setw(35)<<"ID: "
                <<right<<" "<<item_array[0]<<endl;
            cout<<setfill('.')<<left<<setw(35)<<"Name: "
                <<right<<" "<<item_array[1]<<endl;
            cout<<setfill('.')<<left<<setw(35)<<"Rate: "
                <<right<<" "<<item_array[2]<<endl;
        }
    }

    data.close();
}

it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.