Difference in array and tuple

I am trying to read a txt. file into a tuple. I have done this before with an array and tried to use the same code. I'm not sure what I am missing about the difference between the two types of containers. Here is my code for reference.

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
 typedef tuple<string,int,int,int>mytuple;
int main(int argc, char** argv) 
{
    vector<mytuple>bowl;
    int pos=0;
    ifstream infile("c:\\c++data\\bowlers.txt");
    if(!infile)//verify file is found
    {//top of verify if
        cout<<"cannot open file.";
        cout<<"This program will terminate.";
        return 1;
    }//bottom of verify if
    for(int r=0;r<10;r++)
    {//top of r read
        getline(infile,bowl[r]);
        pos=bowl[r].find_first_of('\r');
        bowl[r].erase(pos,2);
        for(int c=1;c<=3;c++)
        {//top of c read
            infile>>scores[r][c];
        }//bottom of c read
        infile.ignore(100,'\n');
    }//bottom of r read
    infile.close();
    return 0;
closed account (SECMoG1T)
1
2
3
4
getline(infile,bowl[r]);///???? bowl is an uninitialized vector
                                ///if init bowl[r] would be a tuple not a string
///std::getline reads into a string not a tuple, guess that would be an error


std::getline
http://www.cplusplus.com/reference/string/string/getline/
Last edited on
Ok, so if I understand what I'm reading, I would use something like,
1
2
3
4
5
   
 for(int r=0;r<10;r++)
    {
        bowl.push_back(mytuple(infile));
    }

but it's still throwing errors.
closed account (SECMoG1T)
look at this

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
#include <stringstream>
#include <tuple>
#include <iostream>

typedef std::tuple<std::string,int,int,int> mytuple;

int main(int argc, char** argv)
{
    std::vector<mytuple> bowl;

    std::ifstream infile("c:\\c++data\\bowlers.txt");///assuming the formt is: string,int,int,int

    if(infile)
    {
      std::string temp{},data{};
      int var1{},var2{},var{};
      std::stringstream s_temp;

      for(int i = 0; i<10; i++)
      {
          s_temp.str(""); s_temp.clear();
          std::getline(infile,temp,'\n');///edited

          s_temp<<temp;
          s_temp>>data>>var1>>var2>>var3;

          auto index = data.find_first_of('\r');

          if(index != std::string::npos)
            data.erase(index,2);

          bowl.push_back(std::make_tuple(data,var1,var2,var3));
      }
    }

    else
    {
        std::cout<<"cannot open file.";
        std::cout<<"This program will terminate.";
    }


    return 0;
}
Last edited on
Wouldn't i run into the same problem i had before with line 22: std::getline(temp,infile,'\n');
closed account (SECMoG1T)
ooh, i think i dint check that twice, my bad change it to
1
2
std::getline(infile,temp,'\n'); ///correct


i mis-arranged the args.
Last edited on
Topic archived. No new replies allowed.