i would like to ask for help regarding my problem, it's like this... I have a txt file and inside it looks like this

OL001 bob red 13
OL002 jack blue 13
OL003 paul yellow 14

so if my program starts it will ask for the OLXXX and return each line like this

OL#:OL002
Name: jack
Color: blue
Age: 13

I dont know where to start, since I tried using arrays (0~2) but i dont have an idea how to include the OL00s

*I'm planning on using structures for the second part, I just cant figure out how to the OL# searching and returning

Just to clarify, are you reading your data into the program or is the user typing it in? However, in reality your struct should be the same either way you do it. OL# can be a string member of your struct, or you could make it an int member and strip the OL out of the string you read in and turn the remainder into an integer.

hello, the data are read into the program. Is it possible to use a for loop and compare the user input to the structure like if (userinput = s1.id)?

Is it possible to use a for loop and compare the user input to the structure like if (userinput = s1.id)?

Yes with == ( if you have a pointer to the struct it would be userinput ==mystruct->id)

ok i'm giving it a shot . . .

#include <fstream>
#include <iostream.h>
using namespace std;

main()
{
char buffer[256];
ifstream myfile ("data.txt");
// Declaring people structure
struct people{
char name[64];
char color[64];
int age;
int id;

};

 myfile.getline (buffer,100);
people s1 = {buffer};
cout << "Displaying the data members" << endl;
cout << "The name is " << s1.name << endl;
cout << "The color is " << s1.color << endl;
cout << "The age is " << s1.age << endl;
cout << "The id is " << s1.id << endl;

}

its not working yet but i have to start with something, what do you think im doing wrong in this one?

commented: Good attitude, makes a change from all the "plzsendcode" losers +17

Couple of things right off the bat... #include <iostream> the one with the .h is potentially a prestandard version. main() should return an int (and pop a return 0; at the end of main.

If you can I would include <string> and use that instead of the char array. If you can't we can work around it. That way with the string you can read right into it (you can use getline but with the mixed strings and ints in your data you're probably better of using myfile >> . See this and the related pages about fstream.

#include <fstream>
      #include <iostream>
      #include <string>
      using namespace std;
       
      main()
      {
      char buffer[256];
      ifstream myfile ("data.txt");
      // Declaring people structure
      struct people{
      char name[64];
      char color[64];
      int age;
      int id;
      };
       
      myfile.getline (buffer,100);
      people s1 = {buffer};
      cout << "Displaying the data members" << endl;
      cout << "The name is " << s1.name << endl;
      cout << "The color is " << s1.color << endl;
      cout << "The age is " << s1.age << endl;
      cout << "The id is " << s1.id << endl;
return 0;
      }

about the extraction operator are you suggesting i should do the myfile like this myfile>>(buffer,100)?

i cant seem to transfer the line of text to the struct, do you have any ideas where to start?

I would skip over the buffer completely.
Make name and color strings (e.g. string name; )
Create a loop and read:
myfile >> s1.name; //but see below, should be s1.name
myfile >> s1.color;
(get the int)
(get the other int)
then repeat this over and over again for all the lines of your file. So you should make an array of people actually.

Oh yeah, and qualify main() with int (e.g., int main() ).

commented: hes a good programmer +1

i think i get what you're saying a bit, can you check this one please

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{

ifstream myfile ("data.txt");
// Declaring people structure
struct people{
string name[64];
string color[64];
int age;
int id;
};

while (! myfile.eof() )
{

myfile>>s1.name;
myfile>>s1.course;
myfile>>s1.age;
myfile>>s1.id;
}
return 0;
}

after reading your post, i shouldve done a for loop sorry, ill try that making a new one

No no, while is actually preferred. You can use your cin >> s1.name as the condition for the while loop.

Great on the string changes, but now the whole string will be stored in that variable, so you don't need 64 of them in one struct.

Also remember to make an array of structs as each line in the data file will have its own. As it stands you'd overwrite your variables each time.

jonsca im doing great thanks to your guidance, but i still have one dilemma

ifstream myfile;
     myfile.open("sample.txt");

    for (int i = 0; i < 5; i++)
    {


    myfile>>s1[i].name;
    myfile>>s1[i].color;
    myfile>>s1[i].age;
    myfile>>s1[i].id;



    if (id == s1[i].id)
    {
    cout<<s1[i].name;;
    cout<<s1[i].color;
    cout<<s1[i].age;
    cout<<s1[i].id;
    cout<<endl;
    }



    }
     inData.close();

im trying to compare the user input ID to my txt file ID so that the program can return all the associated data corresponding to the ID. This is my last problem, I hope you can guide me through it

What's the error that you are getting? Are there really only 5 records in the file?

aah its just a sample so i set it to 5, the error looks like this

request for member `name' in `s1', which is of non-class type `people*'

do i need pointers for this kind of stuff? because i dont think that "s1.id" can be compared with "id"

You can try it with the -> but let me see what the whole thing looks like.

ok here goes

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

string  id;

struct people
{
     string name;
     string color;
     int age;
     int id;


};


void init (people s1[]);


int main()
{

  people s1[5];

  init(s1);

 system ("PAUSE");
 return 0;
}

void init (people s1[])
{

     ifstream inData;
     inData.open("sample.txt");
 cout<<"enter id :";
  cin >>id;
    for (int i = 0; i < 5; i++)
    {


     myfile>>s1[i].name;
     myfile>>s1[i].color;
     myfile>>s1[i].age;
     myfile>>s1[i].id;

    if (id==s1[i].id)
    {
        cout<<"found!";
    }

    }
     inData.close();
}

there it is can u see whats wrong?

Ok. I would take string id out of the global scope and just declare it locally in the body of the function. Also, it should be an int since that's what s1.id is. If you really need it elsewhere in the program, declare it in main and pass it in by reference to the function.

You have a mismatch between inData and myfile. There should only be one name for the infile stream used in this case.

Finally, you can substitute cin.get(); for system("pause");for a more portable solution (see this). EDIT: also, substituting using std::cout; using std::endl; etc for using namespace std; keeps your namespace less polluted.

EDIT II: You need to rearrange your input sequence to match your file and take care of the OLs (do you want them or not?).

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;



struct people
{
     string name;
     string color;
     int age;
     int id;


};


void init (people s1[]);


int main()
{

  people s1[5];

  init(s1);

 cin.get();
 return 0;
}

void init (people s1[])
{
  int  id;
     ifstream myfile;
     myfile.open("sample.txt");
 cout<<"enter id :";
  cin >>id;
    for (int i = 0; i < 5; i++)
    {


     myfile>>s1[i].name;
     myfile>>s1[i].color;
     myfile>>s1[i].age;
     myfile>>s1[i].id;

    if (id==s1[i].id)
    {
        cout<<"found!";
    }

    }
     myfile.close();
}

im getting crazy just thinking about it hahaha, btw the cin.get is a nice touch thanks. Do you have anything to add? or is this cool?

//first line of the file OL001 bob red 13
myfile>>s1.name; //OL001 so far so good but this is the ID
myfile>>s1.color; //bob still "ok" at least matches in type
myfile>>s1.age; //red not good, what is the number red?
myfile>>s1.id; // 13 okay matches but red already corrupted this

ah~ the arrangement, so that's the bug! many thanks jonsca I think everything is solved, you are a reaaaal big help i did not knew c++ was so fun. It's my 2nd day teaching myself this language so thanks for keeping up with me.

* I would like to add more to you rep I think im not allowed to do it twice a day haha

Hehe. No worries. You're doing pretty darn good for your second day!

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.