i have to make a program that facilitates the returning of lost pets to their owners, my current step to start is to make a function that populates the struct.

here is my current work:

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

using namespace std;

const int SIZE = 100;

struct petRecord
    {
        char status[6];
        char type[4];
        char gender[8];                // sizes are just gueses
        char breed[20];
        int age;
        char colour[20];
        char suburb[20];
        double phoneNo[50];
    };
    
    petRecord PetID[SIZE];
    
    
    void readFile();
    
    int counter;
    
    char ch;
           
int main()
{

 readFile();
         
 
         
             
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
} 

void readFile()
{
             char ch;

ifstream inFile("pets.txt"); // opens file, prints contents = D

while(!inFile.eof())
{
 for (counter = 0; counter < 100; counter++)
     {
              inFile.get (ch);
              inFile.getline( PetID[counter].status, ' \n ');
              inFile.getline(PetID[counter].type, ' \n ')
              inFile.getline(PetID[counter].gender, ' \n ')
               inFile.getline(PetID[counter].breed, ' \n ')
                inFile.getline(PetID[counter].age, ' \n ')
                 inFile.getline(PetID[counter].colour, ' \n ')
                  inFile.getline(PetID[counter].suburb, ' \n ')
 
         
         }
           
   /*  for (counter = 0; counter < 100; counter++)
     {
         cout   << PetID[counter].status << " ";
                //<< PetID[counter].type << " "
                //<< PetID[counter].gender << " "               // attempt to print info to see if it is storing
                //<< PetID[counter].breed << " "
                //<< PetID[counter].age << " "
                //<< PetID[counter].colour << " "
                //<< PetID[counter].suburb << " ";
                //<< PetID[counter].phoneNo << " ";
                }
        */ 

}

i have to populate info from a text file, which looks like this:

lost
cat
female
persian
0 3
black-white
mangerton
42614297
found
cat
male
siamese
5 6
tan
balgownie
42898942
lost
dog
female
pomeranian
5 4
black
dapto
42354461
found
cat
male
tiger
0 6
black-white
port kembla
42342706
lost
dog
female
cattle dog
5 1
white
dapto
42940424


my question is, am i on the right track? if not can you help point me the right way
if i am on the right track, where should i go from here, thanks in advance for assistance.

That version of getline needs a streamsize, so for example inFile.getline( PetID[counter].status, ' \n '); should be inFile.getline( PetID[counter].status,6, ' \n '); (6 being the size of the status array) and really you can leave off the '\n' as that is the default.

You're not taking in the phone number in your input loop at all.

Also, why are you reading a character before reading the status?

The best test of all this is to do exactly what you planned to do in the comment section, print the records out (though probably 3-4 initially instead of all 100).

Edited 5 Years Ago by jonsca: n/a

ok, i'll give that a shot. the character reading, was from when i was testing if it was reading the file, i used it to print the contents of the file, forgot to take it out.

with printing the records, is the commented section along the correct lines?

thanks for your input, it is much appreciated

Yeah, reading that character is probably throwing everything off.

with printing the records, is the commented section along the correct lines?

Yes, looks okay. cout can handle the C-string.

ok got the output somewhat correct, how ever my program doesnt want to compile when i use an int for age, get a no matching function for call error, sorry if im asking too many questions.

ok got it going for the first set, now i need to get it to store multiple entry's up to 100, i thought the code i had would do that, but it doesnt appear so, any tips on where to go from here?

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

using namespace std;

const int SIZE = 100;

typedef struct petRecord
    {
        char status[6];
        char type[4];
        char gender[8];
        char breed[20];
        char age[3];
        char colour[20];
        char suburb[20];
        char phoneNo[50];
    };
    
    petRecord PetID[SIZE];
    
    
    void readFile();
    
    int counter;

           
int main()
{

 readFile();
         
 
         
             
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
} 

void readFile()
{

ifstream inFile("pets.txt"); 

 for (counter = 0; counter < 100; counter++)
     {
 
              inFile.getline( PetID[counter].status,6);
             inFile.getline(PetID[counter].type, 4);
              inFile.getline(PetID[counter].gender, 8);
               inFile.getline(PetID[counter].breed, 20,'/0');
                inFile.getline(PetID[counter].age, 3);
                 inFile.getline(PetID[counter].colour, 20);
                  inFile.getline(PetID[counter].suburb, 20,'/0');
                  inFile.getline(PetID[counter].phoneNo, 50);
         
         }
         
 for (counter = 0; counter < 100; counter++)  
     {  
         cout   << PetID[counter].status << " "
                << PetID[counter].type << " "
                << PetID[counter].gender << " "              
                << PetID[counter].breed << " "
                << PetID[counter].age << " "
                << PetID[counter].colour << " "
                << PetID[counter].suburb << " "
                << PetID[counter].phoneNo << " ";             
      }
}

my current code, it all it does is return the first set of data in the list

lost
cat
female
persian
0 3
black-white
mangerton
42614297

and the end, i have a feeling it has something to do with my petID not being correct, but im not sure where to go from here.

Why do you have the '/0's on 56 and 59? I'm not sure that's it, though. Try declaring counter local to your loop instead of global. for(int counter = 0;etc.) Print out counter to see how far you get in the list when you're reading.

the /0's are there because in the text file there is a digit that has to be ignored for age, without them, for the first entry it shows age as 0 instead of 3.

ok moved the counter local, and had it print the counter, it prints the counter fine, but still only prints the first record. it doesnt seem to be making a new record under the pedID
l'll try a fhew things and post back.

Yes, I forgot about that 0 3 thing. Read the line into a temp and extract the digits

char temp[3];
inFile.getline(PetID[counter].breed, 20);
inFile.getline(temp, 4);
PetID[counter].age = function to change temp to an int //check that this works for all of your cases
//like what does it look like if the pet is 14?  1  4  or 014?
inFilegetline(PetID[counter].colour, 20);

Edited 5 Years Ago by jonsca: n/a

ok i got it working, now, its recording all the data, and it is all showing when being printed, i now need to have something that says how many records are in the array, i tried using counter but that seems to just say 100, ie the max number of records, any tips?

So the number might be less than 100? You'll need to turn it back into a while loop.
Eliminate line 50, and substitute in while(inFile.getline( PetID[counter].status,6)) (eliminate line 53). infile.getline() will "drive" the loop and return a null value after the file is read). To keep track of the number of records, keep a counter (say, "recordno") and increment it at the end of the while loop. Then, run your for loop on line 64 to recordno instead of 100.

ok tried to work around this abit, diddnt get anywhere, are you able to give me some outlines on how to go about this? again, your help is very much appreciated.

This is essentially what jonsca is telling you to convert your algorithm to:

int readRecords = 0;
while (/*read first line of pet record*/) {
  //read and process remaining lines of current pet record;
  ++readRecords;   //increment your record counter
}

for (int i = 0; i < readRecords; ++i) {
  //output current record
}

Edited 5 Years Ago by Fbody: n/a

thanks for the assistance, got the first 3 parts working now.

i now have to add new information to my text file.

and this is where i have gotten.

void addRecord()
{
    char status,type,colour,suburb;
    char breed;
    char age, phoneNo;

    ofstream fout;

    fout.open ("pets.txt", ios::app);

    cout << "Is your pet lost or found? (l/f): ";
    cin >> status;
    fout << status;


   // do this for each variable.

    fout.close();

}

a fhew questions i have are

how do i make it add to the end of my file, at the moment it seems to be adding a fhew empty lines before the end of my file and the new data being put in, this is screwing with my array when reading in the file.

and how do i make any user input asked for, if left blank, put in unknown instead.

char status,type,colour,suburb;
char breed;
char age, phoneNo;

Char is only going to give you, well, a char. You need to make those arrays, too. Ballpark it, so if someone's pet's name is "fjdskljfskljflskjffsfsfd" (which is a great name) you only take the first 10 chars or so anyway. Hint, use getline.

Test to see if your char arrays are empty before writing "unknown". Since you are using char arrays, you'll need the functions in <cstring>.

Are you trying to append to your existing file that you've been reading from? Check and make sure there aren't excess newlines in the file at the end (open it in a text editor to see).

ok got that working thanx.

now it is still not adding a new line when an entry is put in e.g

(lastlineofmyfile)<<inputs data starting here.
<< i want it to start here.
here is my current code.

void addRecord()
{
    char status[25],type[25],colour[25],suburb[25];
    char breed[25];
    char age[25], phoneNo[40];
    char s1[]= "unknown";
    char s2[20];

    ofstream fout;

    fout.open ("pets.txt", ios::app);

    cout << "Is your pet lost or found? (l/f): ";
    cin.getline(status,25);

    if (status[0] == '\0')
    {
        strcpy ( status, s1);

    }
    fout << status << endl;


    cout << "Is the pet a Cat or a Dog? (d/c): ";
    cin.getline(type,25);
    fout << type << endl;
    cout << "What breed is the pet? ";
    cin.getline(breed, 25);
    fout << breed << endl;
    cout << "What age is the pet? (yy mm)";
    cin.getline(age, 25);
    fout << age << endl;
    cout << "What colour is the pet? ";
    cin.getline(colour,25);
    fout << colour << endl;
    cout << "In what suburb was the pet lost? ";
    cin.getline(suburb, 25);
    fout << suburb << endl;
    cout << "What is your phone number? ";
    cin.getline(phoneNo, 40);
    fout << phoneNo << endl;



    fout.close();


}

nevermind i figured it out, something really stupid i was doing wrong haha

another bit of help needed.

i have to search my array and 'remove' any non similar matches.

im guessing using the strcmp? to compare them, then if they are different, make that array element = NULL? and reprint at the end?

non similar matches

I'm not exactly sure what you mean by that.

Just use strcpy with a blank string to "erase" that array element.

basically if i search for, black dog border-collie, it eliminates anything that doesnt match black dog border-collie, and returns how many records match the search, if that makes any more sense.

Yeah, I see what you are saying. You just have to know what position the field you are seeking is in, so you can go up to the start of that group and start clearing.

lost                <---------------------------------
cat       Go back n spaces,clear on down to mangerton|
female                                               |
persian >------- Search is for persian, this matches |
0 3
black-white
mangerton
42614297

Edited 5 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.