samedi 16 janvier 2021

how to ensure proper indentation in datas ifstreamed from a file. Look at my code , it's not giving desired results

if(role==2)
    {
        int arr;
        ifstream record("total.txt");
        record>>arr;
        record.close();
        string names[arr];
        int age[arr];
        ifstream file("names.txt");
        ifstream file0("ages.txt");
        for(int i=0;i<arr;i++)
        {
            getline(file, names[i]);
            file0>>age[i];
        }
        for(int i=0;i<arr;i++)
        {
            cout<<names[i]<< " "<<age[i]<<endl;

        }
    }

Here I am having 2 files contaning names and ages of some person. File names total.txt contains a number which is total number of persons. Flies named names.txt and ages.txt contains names and ages one in each row.

ages.txt:                names.txt:
18                       anil kumar
25                       naman ojha
27                       vishnu pandit
52                       sangakara g
48                       helen keller

I got the result of my above written code as:

anil kumar 18
naman ojha 25
vishnu pandit 27
sangakara g 52
helen keller 48

results which I wanted :

anil kumar                      18
naman ojha                      25
vishnu pandit                   27
sangakara g                     52
helen keller                    48

How to achive this result? New ideas are welcomed.

Aucun commentaire:

Enregistrer un commentaire