Good day!
I am currently developing a notepad storage to hold the players information!
In notepad I have this data: Where data is seperated by a comma. First is the playerID, PlayerName, PlayerScore.

202, Andrie Velez, 4500
203, Joseph Josh, 3500
200, Carl J0sefana, 4000

Ive created a sample code to generate some result but its not totally working, here is my code so far:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char playerID[3];
ifstream Players("Players.txt");
   if (Players.is_open()}
      {
         cout<<"Enter Player ID number: ";
         cin>>playerID;
         while(!Players.eof())
            {
             //code to  compare in Players.txt base on given playerID and display the PlayerName and       PlayerScore if playerID match in Players.txt
            }
            Players.close();
       }
       return 0;
      }

Any help is greatly appreciated!

Recommended Answers

All 11 Replies

And yet another "not totally working" post, as if we're supposed to know what that means. When asking for help, give us information. Don't make us guess what "not working" means.

One of the way to get through this problem is using classes. There you can write and read an object through filestream.write() and filestream.read() methods.
Else this is also a viable solution

while(!Players.eof())
{
 Players.getline(line,20,',');

        if (!strcmp(line, playerID))
        {      
            while (true)
            {
             Players.getline(details, 20, '\n');
             cout << details << endl;

             if (strcmp(details, terminator) == 0)      // terminating condition
                break;
            }

        }
}

Here terminator is nothing but a string that marks the end of details of one player.

Sorry WaltP for my uncleared post.

The situation is, I want to search in the Players.txt information base on the PlayerID inputed by the user. So if the user has make input of "203" as playerID base on the example above, it should display the PlayerName which is "Joseph Josh" and the PlaterScore which is "3500".

@ np complete Thank you!

May I just ask what is the datatype of the variable that you used like line, details,terminator. Its not working on my side. Maybe the data type of this variable I am declaring?

I used them as string / character array. "details" means the details of players that you want to display. And "terminator" is just a flag / indicator that a complete players data have been read. It marks the end of data of players.
I just gave you a push in the right direction, by providing a sample code. Now try to figure out how you can use this in your program. Google getline for more info on it.
Perhaps this discussion may help you.
Good luck !

Thank you np complete!

I currently have this working code base on your example and suggestions. But I need this to be more precise/less coding. Can this be revised and have less codes? And also, can I convert my char variable to string? I dont want to initialize variable like char[100] since it allocate memory not the actual size of the data being stored.

#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
    stringstream line;
    char PlayerData[100];
    char PlayerName[50];
    char PlayerScore[6];
    char respcode[2],PlayerID[2];
    ifstream Players("Players.txt");
    if(Players.is_open())
    {
         cout<<"Enter Player ID Number: ";
         cin>>respcode;
          while (!Players.eof())
          {     
                Players.getline(PlayerData,sizeof PlayerData);
                line<<PlayerData;
                line.getline(PlayerID,2,',');
                line.getline(PlayerName,50,',');
                line.getline(PlayerScore,6,'\n');
                line.clear();

                if(strcmp(respcode,PlayerID)==0)
                {
                   cout<<"Player "<<PlayerName<<" has score of "<<PlayerScore<<endl;
                   break;
                }                         
          }
          line.clear();
          Players.close();

    }
    else
    {
        cout<<"file not found"<<endl;
    }
system("pause");
return 0;
}

I want to achive the same output with less/precise code.

Thank you!

This might be a short one. But I get this error "22 invalid static_cast from type std::basic_istream<char, std::char_traits<char> >' to typechar[2]".

Here is my revised code:

#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
    stringstream line;
    char PlayerData[100];
    char PlayerName[10];
    char respcode[2],PlayerID[2];
    ifstream Players("Players.txt");
    if(Players.is_open())
    {
         cout<<"Enter Employee Code: ";
         cin>>respcode;
          while (!Players.eof())
          {     
                Players.getline(PlayerData,100,',');
                line<<PlayerData;
                  if (strcmp(static_cast<char>(line.getline(PlayerID,2,'\n')),respcode)!=0)
                   {
                     continue;
                   }
                   else
                   {
                   cout<<"Player found in text database";
                   break;
                 }                    
          }

          line.clear();
          Players.close();    
    }
    else
    {
        cout<<"file not found"<<endl;
    }
system("pause");
return 0;
}

Anybody knows where the problem is? Ive use static cast to make sure the type is the same during strcmp but no luck..

Anybody knows where the problem is?

It's on (or near) the line mentioned in the error. Unfortunately, when you don't tell us the line, or indicate the line because the post numbering is different than your code numbering, it kinda hard to pinpoint the where. Please remember to post all pertinant information necessary to help us help you.

Now in this case, I see a major faux pas. On the line
if (strcmp(static_cast<char>(line.getline(PlayerID,2,'\n')),respcode)!=0)
(which I suppose is your error line) why are you
1) reading data
2) calling a function
3) casting data
4) testing data
all in one line? Split it up into multiple appropriate statements so you can see exactly what you are doing. This often helps in understanding not only the code but any problems that arise.

Thank you WaltP!

Im trying to make the code more preciess/less? But maybe my solution above is the lesser one?

Yes it is. Your solution is leaning toward code you can find at this site. Most is quite precise, but.... ;o)

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.