jeudi 26 juillet 2018

How to Initialize Integers + String Arrays

I'm new to C++ and need help! I'm currently learning how to do 2D arrays or just 1D arrays.

I have a text file with the following contents shown below, and I'm basically trying to store them into an array or more depending? Is it true that I can have just 1 Array if I use Struct, or 5 individual Arrays based on the contents in the file below?

I believe that the [1, 2] can be formed into a 2D Array, but how do I go about implementing it?

Below are my codes for using a struct, and I'm not sure if I did it right?

Please help!

=========================================================
Sample Contents in 'HelloWorld.txt' File
=========================================================
[1, 2]-3-4-Hello_World
[5, 6]-7-8-World_Hello
[9, 1]-2-3-Welcome_Back
[4, 5]-6-7-World_Bye
[8, 9]-1-2-Bye_World

=========================================================
My Sample Codes
=========================================================
struct Example()
{
    fstream file;
    file.open("HelloWorld.txt");

    vector<string> abc; 
    string line;

    while(getline(file, line, '-'))
    {
        abc.push_back(line);

        int** a = new int*[abc.size()];
        for(int i = 0; i < abc.size(); i++)

        cout << abc[i] << endl;

        delete [] a;
    }
}

My main objective is to be able to store all the 4 integers + 1 string into arrays, and learn how to create a 2D array with the '[1, 2]' -> the first two integer points.

Awaiting advice! Thank you!

Aucun commentaire:

Enregistrer un commentaire