txt file input to parallel array

I'm pretty green at programming. I've been trying to figure out how to do this for a few hours a day for the past few days. The professor hasn't emailed back yet.

The assignment is to input this file;

Luke Donald
70
68
71
69
Ernie Els
67
70
68
68
Adam Scott
64
67
68
75
Brandt Snedeker
66
64
73
74
Tiger Woods
67
67
70
73


into a parallel array. Then I have to create a few procedures to find total score, lowest scores, and to identify the winner.

I've searched "input txt to parallel arrays" and similar things for hours. Our book only explains raptor, and his examples are for simple arrays, not parallel containing both strings and integers.

I had the code below working to where it would give me the exact file above. I'm having a really hard time getting it to work. I've left things that I've tried as notes. With it like this, I just get a bunch of random really long numbers.

Could anyone give me a clue or two on how to get the data into a parallel array so I can work with it?

Thanks!!





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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
   
  
  
    ifstream myfile ("BritishOpen2012.txt");
    if (myfile.fail())

               { cout<<"Error! File not found.";
                system("PAUSE");
                exit(1);
                }
  
  
    string name[5];
    int score[5][4];
    
    for(int x=0;x<5;x++)
    {
            myfile >> name[x];
            
            //cout << "Working";  //to test if it works
    
            for(int y=0;y<4;y++)
    
             cout << score[x][y];
         } 
    
    
    
    
    system("PAUSE");
    return 0;
  
  
  
  
  
  /*   for(int i=1,i<=4,i++)
     {
             myfile>>name;
             myfile>>score1>>score2>>score3>>score4;
     }

     for(int i=1,i<=4,i++)
     {
             cout<<name;
             cout<<score1<<score2<<score3<<score4;
             cout<<"IT WORKS";
             }
    */ 
     
}
closed account (D80DSL3A)
It looks like it would work if you change line 33 to myFile >> score[x][y];.
I couldn't figure it out that way. After a little more research I've decided to try it like 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
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;


void totalScore (string[], int[], int[], int[], int[], const int);

int main()
{
   
const int ARRAY_SIZE = 5;
string names[ARRAY_SIZE] ;
int score1[ARRAY_SIZE];
int score2[ARRAY_SIZE];
int score3[ARRAY_SIZE];
int score4[ARRAY_SIZE];
int count = 0;
ifstream inputFile;
  
  inputFile.open("BritishOpen2012.txt");
  
  while(count < ARRAY_SIZE)
              {
                   inputFile >> names[count];
                   inputFile >> score1[count];
                   inputFile >> score2[count];
                   inputFile >> score3[count];
                   inputFile >> score4[count];
                   cout << names[count] ;
                   cout << score1[count];
                   cout << score2[count];
                   cout << score3[count];
                   cout << score4[count];
                   count++;
              }

/* void totalScore(string names[], int score1[], int score2[], int score3[], int score4[], const int ARRAY_SIZE)
 {    int count;
      int sum=0;        
    
    for(count = 0; count < ARRAY_SIZE; count++)
    { sum[count] = score1[count] + score2[count] + score3[count] + score4[count];
    count++;
}
    
  */  
    system("PAUSE");
    return 0;
}


/*
Luke0208987794772020092525792293564406323200208987161622929444077600407752920898
775682089877952162368400-122935364077608Press any key to continue . . .
*/


However, all I get is the few lines at the bottom.

I was also having trouble with the total of the scores for each player. Kept getting a compiler error of int[5]...int[5]...operator+...


I'm stumped.
closed account (D80DSL3A)
Problem is your players have 2 names each. inputFile >> names[count]; reads the 1st name only. The next line inputFile >> score1[count]; then chokes to death on the character input (of the 2nd name).

Easiest fix is to use another array of string lastNames[ARRAY_SIZE];
If you must read both the 1st and last name into one string then you'll have to start mixing formatted and unformatted input methods, which is a pain.

EDIT: OK. Got it. You can use getline(istream, string); to read both names into one string. The inputFile >> scorex[count]; lines are ok until it's time to read another name.
There's a newline character waiting to choke the life out of inputFile, so we must burn it with inputFile.ignore(256,'\n');// burn the \n . This code is working on your test data:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while(count < ARRAY_SIZE)
              {
                  std::getline( inputFile, names[count] );// read both names into names[count]
                //   inputFile >> names[count];
                   inputFile >> score1[count];
                   inputFile >> score2[count];
                   inputFile >> score3[count];
                   inputFile >> score4[count];
                   inputFile.ignore(256,'\n');// burn the \n before attempting to read the next name
                   cout << names[count] << " ";
                   cout << score1[count] << " ";
                   cout << score2[count] << " ";
                   cout << score3[count] << " ";
                   cout << score4[count] << '\n';
                   count++;
              }

Perhaps someone who knows the art of console input better can outline a better (or correct) method for you.
Last edited on
> inputFile.ignore(256,'\n');// burn the \n before attempting to read the next name

Yes. An alternative is to use the input stream manipulator std::ws

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 <iostream>
#include <sstream>
#include <string>

int main()
{
    std::istringstream file(
                              "Luke Donald\n 70 \n 68 \n 71 \n 69 \n "
                              "Ernest \"Ernie\" Els\n 67 \n 70 \n 68 \n 68 \n "
                              "   Adam Scott\n 64 \n 67 \n 68 \n 75 \n "
                              "Brandt Snedeker\n 66 \n 64 \n 73 \n 74 \n "
                              "Eldrick Tont \"Tiger\" Woods\n 67 \n 67 \n 70 \n 73 \n "
                           ) ;

    constexpr std::size_t NPLAYERS = 5 ;
    constexpr std::size_t NSCORES = 4 ;

    std::string name[NPLAYERS] ;
    int score[NPLAYERS][NSCORES] {{0}} ;

    for( std::size_t i = 0 ; i < NPLAYERS ; ++i )
    {
        // read the full name after skipping any leading whitespace
        // http://www.cplusplus.com/reference/string/string/getline/
        // http://www.cplusplus.com/reference/istream/ws/
        std::getline( file >> std::ws, name[i] ) ;

        // read the scores
        for( int& s : score[i] ) file >> s ;
    }

    if(file) // if the reads were successful
    {
        // print out the contents of the two arrays
        for( std::size_t i = 0 ; i < NPLAYERS ; ++i )
        {
            std::cout << "name: '" << name[i] << "'    scores: " ;

            for( int s : score[i] ) std::cout << s << ' ' ;
            std::cout << '\n' ;
        }
    }

    else std::cerr << "error reading file \n " ;
}

http://coliru.stacked-crooked.com/a/33228fb39e68d0a7
I've got it to where it inputs the data. I also got it to calculate the totals for each player. Thanks for the help so far.

I've tried a few "methods" of calculating the lowest, but I'm not sure of how to pass the sum variable/array to the procedure. Am I on the right track?

Thanks!


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


void totalScore (string[], int[], int[], int[], int[], const int);
void findLowest (string[], int[], const int);

int main()
{
   
const int ARRAY_SIZE = 5;
string names[ARRAY_SIZE] ;
string lastnames[ARRAY_SIZE];
int score1[ARRAY_SIZE];
int score2[ARRAY_SIZE];
int score3[ARRAY_SIZE];
int score4[ARRAY_SIZE];
int count = 0;
ifstream inputFile;
  
  inputFile.open("BritishOpen2012.txt");
  
  while(count < ARRAY_SIZE)
              {
                   inputFile >> names[count];
                   inputFile >> lastnames[count];
                   inputFile >> score1[count];
                   inputFile >> score2[count];
                   inputFile >> score3[count];
                   inputFile >> score4[count];
                   cout << names[count] ;
                   cout << lastnames[count]<< endl ;
                   cout << score1[count]<<endl;
                   cout << score2[count]<<endl;
                   cout << score3[count]<<endl;
                   cout << score4[count]<<endl;
                   count++;
              }

void totalScore(string names[], string lastnames[], int score1[], int score2[], int score3[], int score4[], const int ARRAY_SIZE);
 {    int count;
      int sum;       
      int lowest =0;
      
    for(count = 0; count < ARRAY_SIZE; count++)
    { sum = score1[count] + score2[count] + score3[count] + score4[count];
      cout << names[count] <<  lastnames[count] << endl << sum <<endl;


         }}
/*void findLowest(string names[], string lastnames[], int* sum[], const int ARRAY_SIZE);    
    int sum;
    int lowest =sum[0];
    
    for(count=0; count < ARRAY_SIZE; count++)
    {if(lowest>sum[count])
         {lowest=sum[count];
         cout <<lowest ;
         }}
 
  */  
    
    
    
    
    
    
    
    system("PAUSE");
    return 0;
}


/*
LukeDonald
70
68
71
69
ErnieEls
67
70
68
68
AdamScott
64
67
68
75
BrandtSnedeker
66
64
73
74
TigerWoods
67
67
70
73
LukeDonald
278
ErnieEls
273
AdamScott
274
BrandtSnedeker
277
TigerWoods
277
Press any key to continue . . .
*/
I changed

int sum;
to
int sum[count];

and it returns 108 now??
I figured out how to get it to find the lowest score! Any hints on how to get it to display the name associated with that score?




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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


void totalScore (string[], int[], int[], int[], int[], const int);
void findLowest (string[], int[], const int);

int main()
{
   
const int ARRAY_SIZE = 5;
string names[ARRAY_SIZE] ;
string lastnames[ARRAY_SIZE];
int score1[ARRAY_SIZE];
int score2[ARRAY_SIZE];
int score3[ARRAY_SIZE];
int score4[ARRAY_SIZE];
int count = 0;
ifstream inputFile;
  
  inputFile.open("BritishOpen2012.txt");
  
  while(count < ARRAY_SIZE)
              {
                   inputFile >> names[count];
                   inputFile >> lastnames[count];
                   inputFile >> score1[count];
                   inputFile >> score2[count];
                   inputFile >> score3[count];
                   inputFile >> score4[count];
                   cout << names[count] ;
                   cout << lastnames[count]<< endl ;
                   cout << score1[count]<<endl;
                   cout << score2[count]<<endl;
                   cout << score3[count]<<endl;
                   cout << score4[count]<<endl;
                   count++;
              }

void totalScore(string names[], string lastnames[], int score1[], int score2[], int score3[], int score4[], const int ARRAY_SIZE);
 {    int count;
      int sum[4];       
      int lowest =0;
      
    for(count = 0; count < ARRAY_SIZE; count++)
    { sum[count] = score1[count] + score2[count] + score3[count] + score4[count];
      cout << names[count] <<  lastnames[count] << endl << sum[count] <<endl;


         }}
         

void findLowest(string names[], string lastnames[], int sum[], const int ARRAY_SIZE);    
    int sum[4];
    int lowest= sum[0];
    
    for(count=0; count < ARRAY_SIZE; count++)
    {if(sum[count]<lowest)
         lowest=sum[count];

         }
   cout << "Lowest score is " << lowest <<  endl;
   
 //"Player is " <<names[count]<< " " << lastnames[count] <<
    
    
    
    
    
    
    
    system("PAUSE");
    return 0;
}


/*
LukeDonald
70
68
71
69
ErnieEls
67
70
68
68
AdamScott
64
67
68
75
BrandtSnedeker
66
64
73
74
TigerWoods
67
67
70
73
LukeDonald
278
ErnieEls
273
AdamScott
274
BrandtSnedeker
277
TigerWoods
277
Lowest score is 273
Press any key to continue . . .
*/
closed account (D80DSL3A)
The trick is to save the index to the lowest...
1
2
3
4
5
6
7
8
9
int indexToLowest= 0;
    
    for(count=0; count < ARRAY_SIZE; count++)
    {if(sum[count]<sum[indexToLowest])
         indexToLowest = count;

         }
   cout << "Lowest score is " << sum[indexToLowest] <<  endl;
   cout << "Player is " << names[indexToLower] <<  " " << lastNames[indexToLower] << endl;
Last edited on
Thanks so much! It worked!

I was trying to build something based off of this...

http://www.dreamincode.net/forums/topic/44658-how-to-return-an-array-index-with-a-function/
Topic archived. No new replies allowed.