How to store strings into an array to later be called back...

I'm trying to store strings into an array of strings (string myArray [9][3]) and want to have them be called later in my program. For some reason when I try to call back the strings stored in the array it only calls the last element entered. Here is my code for storing the info into the string:

for (int i=0; i < 10; i++)

{
while (getline(info, message))
{
istringstream stream(message);

getline(stream,category, ',');
messagearray[i][0] = category;


getline(stream,Event_Id, ',');
messagearray[i][1] = Event_Id;


getline(stream,s_priority, ',');
messagearray[i][2] = s_priority;


getline(stream,abstract, ',');
messagearray[i][3] =abstract;

cout << endl;
}

I have already defined the variables.
Last edited on
The number that you specify when defining an array is the number of elements, not the highest index.

 
std::string messagearray[10][4];
Last edited on
So all I need to do is edit the messagearray?

So in order to call it back I just have to code:

cout << messagearray[2][4];

As an example. And it should give me what I need?
If you define myArray as string myArray[9][3]
Then the highest indices you can use are myArray[8][2]

In other words indices must be less than length.

So if you define std::string messagearray[10][4];
then you cannot do cout << messagearray[2][4];
but you can do cout << messagearray[2][3];
So what I'm trying to store is something like this:

U,E001, 10, Gabe Newell says development for Half-Life 3 is in progress
C,E090, 9, Tax returns of POTUS leaked by IRS
U,E192, 1, Special Snowflake gets mad over nothing
M,E091, 6, POTUS mad at Tax return leaks
U,E401, 7, Republican shuts down government again because no one agrees with him
M,E230, 3, Sniper shoots duck from 1.5km away BOOM HEADSHOT
C,E365, 8, Banker uses company money in Vegas and spends it all on souvenirs
U,E467, 4, Rick and Morty fan buys an old Szechuan Sauce pack on eBay WUBBA LUBBA DUB DUB
U,E352, 5, PETA member admits he let the dogs out
M,E253, 2, POTUS still thinks he is a big deal

How can I store it into the array? or if anything what is wrong with my code above?
Edit: Each part is a string separated by a comma
Last edited on
what do these characters represent?
U,E001, 10,

it might make more sense to declare a struct that can capture these data into its member fields and read the file into a std::vector<T> where T is the struct's typename
Last edited on
They are all read as strings.
you can try this if you wish but use <iomanip> to align the columns as well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>

struct News
{
    std::string m_category;
    std::string m_Event_Id;
    std::string m_priority;
    std::string m_abstract;
    News(){}
    News (const std::string& category, const std::string& Event_Id,
            const std::string& priority, const std::string& abstract)
    : m_category(category), m_Event_Id(Event_Id), m_priority(priority), m_abstract(abstract){}
};
std::ostream& operator << (std::ostream& os, const News& n)
{
    os << n.m_category << ", " << n.m_Event_Id << ", " << n.m_priority << ", " << n.m_abstract << "\n";
    return os;
}
int main()
{
    std::ifstream inFile {"D:\\test.txt"};
    std::vector<News> vecNews{};
    if(inFile)
    {
        std::string line{};
        while (getline(inFile, line))
        {
            std::istringstream stream{line};
            std::string category{}, Event_Id{}, priority{}, abstract{};
            getline(stream, category, ',')
                && getline(stream, Event_Id, ',')
                    && getline (stream, priority, ',')
                        && getline(stream, abstract);
            if(inFile)
            {
                vecNews.emplace_back(News(category, Event_Id, priority, abstract));
            }
        }
    }
    for (const auto& elem : vecNews)std::cout << elem ;
}
Topic archived. No new replies allowed.