Home > Software design >  There's a lot of 0s showing in my output when I try to read 2 txt files
There's a lot of 0s showing in my output when I try to read 2 txt files

Time:09-29

I'm sorry this is long but I don't know what I should do to solve the problem

For the 0s being outputted I'm guessing its the contents of the other file but in 0s I just don't know how to get rid of them

This is my code:

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

using namespace std;

struct Users
{
    int user_id;
    string fname;
    string lname;
    char gender;
    int age;
    int phone;
    string address;
};

struct Contacts
{
    int user_id;
    int contact_with;
    int contact_start;
    int contact_end;
    int distance;
};

void discard_line(ifstream &in)
{
    char c;

    do{
        in.get(c);
    }while(c != '\n');
}

void users(ifstream &userFile, int size, struct Users user[])
{
    cout << "-----------------------------------------------------\n";
            cout << "UserID\tFname\t Lname\tGender\tAge\tPhone\tAddress" << endl;
            cout << "-----------------------------------------------------\n";
            for(int i = 0; i < size; i  )
            {
                cout << user[i].user_id << "\t" << user[i].fname << "\t" << user[i].lname << "\t" << user[i].gender << "\t" << user[i].age << "\t" << user[i].phone << "\t" << user[i].address << endl;
            }
            //total number of users
}

void contacts(ifstream &contactfile, int size, struct Contacts contact[])
{
    cout << "-----------------------------------------------------\n";
            cout << "UserID\tCon/With\tDuration(s)\tDistance(cm)" << endl;
            cout << "-----------------------------------------------------\n";
            for(int i = 0; i < size; i  )
            {
                int duration = contact[i].contact_end - contact[i].contact_start;
                cout << contact[i].user_id << "\t" << contact[i].contact_with << "\t\t" << duration << "\t\t" << contact[i].distance << endl;
            }
}

int main()
{
    int option;
    const int SIZE = 1000;
    int index = 0;
    Users user[SIZE];
    Contacts contact[SIZE];

    ifstream userFile("users.txt");
    ifstream contactFile("contacts.txt");

    if(!userFile)
    {
        cout << "The file is not found" << endl;
        exit(1);
    }

    if(!contactFile)
    {
        cout << "The file is not found" << endl;
        exit(1);
    }

    discard_line(userFile);
    discard_line(contactFile);

    while(!userFile.eof())
    {
        userFile >> user[index].user_id >> user[index].fname >> user[index].lname >> user[index].gender >> user[index].age >> user[index].phone >> user[index].address;
        index  ;
    }

    while(!contactFile.eof())
    {
        contactFile >> contact[index].user_id >> contact[index].contact_with >> contact[index].contact_start >> contact[index].contact_end >> contact[index].distance;
        index  ;
    }

    users(userFile,index,user);
    contacts(contactFile,index,contact);
        
    return 0;
}

and the output is this:

    -----------------------------------------------------
UserID  Fname    Lname  Gender  Age     Phone   Address
-----------------------------------------------------
1001    Ray     Dixon   M       46      9364652 Lokia
1002    Bryan   Green   M       18      9579302 Drekena
1003    Justin  Dixon   M       33      9353533 Lokia
1004    Lester  Byrd    M       45      9534695 Nasilai
1005    Santos  Larson  M       53      9093177 Vunuku
1006    Bryan   Cobb    M       42      9905139 Narocivo
1007    Eddie   Watson  M       20      9610408 Nabua
1008    Wesley  Barton  M       27      9801864 Nasigatoka
1009    Victor  Mason   M       50      9855386 Nukutubu
1010    Ellis   Cobb    M       24      9389406 Narocivo
1011    Diana   Ross    F       27      9940148 Vunuku
1012    Amanda  Carter  F       43      9506743 Nasilai
1013    Maria   Edwards F       53      9798534 Narocivo
1014    Maria   Jenkins F       34      9352516 Lomanikoro
1015    Louise  Davis   F       55      9812126 Nasilai
1016    Sandra  Sanders F       29      9369570 Tavuya
1017    Bonnie  Roberts F       40      9689234 Nukui
1018    Melissa Harris  F       29      9321235 Drekena
1019    Marilyn Parker  F       56      9409221 Nukui
1020    Bonnie  Lopez   F       43      9342939 Nasigatoka
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0
0                               0       0

I'm trying to remove the 0s but I dont know how to do that. However when I take out the reading for either file it shows the correct output without the 0s

CodePudding user response:

while(!userFile.eof())
{
    userFile >> user[index].user_id >> user[index].fname >> user[index].lname >> user[index].gender >> user[index].age >> user[index].phone >> user[index].address;
    index  ;
}

while(!contactFile.eof())
{
    contactFile >> contact[index].user_id >> contact[index].contact_with >> contact[index].contact_start >> contact[index].contact_end >> contact[index].distance;
    index  ;
}

users(userFile,index,user);
contacts(contactFile,index,contact);

Assuming index starts at zero. Assuming userFile has 10 lines Assuming contactFile has 10 lines

when you populate your user array in the while(!userFile.eof()) you increment index to 10... but then when doing the while(!contactFile.eof()) your index is no longer starting at zero... but rather starting where it left off at 10 and continues to 20

you need an additional index...

int uIndex = 0;
int cIndex = 0;

while(!userFile.eof())
{
    userFile >> user[uIndex].user_id >> user[uIndex].fname >> user[uIndex].lname >> user[uIndex].gender >> user[uIndex].age >> user[uIndex].phone >> user[uIndex].address;
    uIndex  ;
}

while(!contactFile.eof())
{
    contactFile >> contact[cIndex].user_id >> contact[cIndex].contact_with >> contact[cIndex].contact_start >> contact[cIndex].contact_end >> contact[cIndex].distance;
    cIndex  ;
}

users(userFile,uIndex,user);
contacts(contactFile,cIndex,contact);
  •  Tags:  
  • c
  • Related