Hello,

So I am trying to read a text file into an array, but I am having issues breaking it up properly.

Here is my text file:

0 Car Fixed
1 Brake
2 Windshield
3 Tires
4 Engine
5 Doors
6 Transmission

and here is my code:

#include <iostream>
#include <string>
#include <fstream>
#include "CarLot.h"
#include "Main.h"
using namespace std;

struct RepairList {
    int repairCode[12];
    string repairPart[12];
};

void RepairsFile() {
    RepairList rList;
    string list;
    ifstream repairs;
    repairs.open("RepairsKey.txt");

    while (getline(repairs, list)) {
        for (int i = 0; i < 12; i++) {
            if (list.at(0) == '0') {
                rList.repairCode[i] = i;
                repairs >> rList.repairPart[i];
            }
            else if (list.at(1) == '1') {
                rList.repairCode[i] = i;
                repairs >> rList.repairPart[i];
            }
            else if (list.at(2) == '2') {
                rList.repairCode[i] = i;
                repairs >> rList.repairPart[i];
            }
            else if (list.at(3) == '3') {
                rList.repairCode[i] = i;
                repairs >> rList.repairPart[i]; 
            }
            else if (list.at(4) == '4') {
                rList.repairCode[i] = i;
                repairs >> rList.repairPart[i];
            }
            else if (list.at(5) == '5') {
                rList.repairCode[i] = i;
                repairs >> rList.repairPart[i];
            }
            else if (list.at(6) == '6') {
                rList.repairCode[i] = 6;
                repairs >> rList.repairPart[i];
            }
            else if (list.at(7) == '7') {
                rList.repairCode[i] = i;
                repairs >> rList.repairPart[i];
            }
        }
    }

    repairs.close();
}

int main() {
    RepairsFile();

    system("pause");
    return 0;
}

What I need it to do is to read each number and output the part needing to be fixed, but what I keep getting is that it's reading each number and letter and storing each one into the array instead of skipping over the number and getting the part.

So if I go to input 1 it would output Brake, but 3 would output Windshield instead of Tires......

https://www.daniweb.com/community/contribute/3#

What am I missing?! Any input would be helpful.

Thank you!

Instead of trying to balance concurrent arrays, use a custom type(RepairType) to represent each line. This way a vector<RepairType> will give you access to everything you need. Reading the values into the vector becomes fairly simple then:

struct RepairList 
{
    class RepairType
    {
    public:
        int code = 0;
        string part = "";
    };
    vector<RepairType> repairTypes;
    void MakeList( istream& in )
    {
        repairTypes.clear();
        RepairType temp = RepairType();
        while ( in >> temp.code )
        {
            getline( in , temp.part );
            repairTypes.emplace_back( temp );
        }
    }
};

int main()
{
    RepairList rl;
    ifstream in( "text.txt" );
    rl.MakeList( in );
}

I've never used vectors before.... We never learned them. I will read up on it.

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.