Filling A String Array

Can you fill a string array without using for loops? For example, if I wanted to store from grades in a class and only wanted to store a name from each line would I be able to? Example (ignore the extra functions, I'm not done with that)

Unit8NamesScores.txt =
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

#include <iostream>
#include <iomanip>
#include <fstream>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using namespace std;
void fillArray(string names[], int scores[]);
void calculateScore(int scores[], char grades[]);
void outputResults(string names[], int scores[], char grades[]);


int main()
{
	string names[10];
	int scores[10][5];
	char grades[11];

	

}

void fillArray(string names[], int scores[])
{
	ifstream namesAndScores;
	namesAndScores.open("Unit8NamesScores.txt");


	namesAndScores >> names[0];
}

Not sure what your asking, but if you don't want to use for, just read each line. Like the code below

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

using namespace std;
void fillArray(string names[], int scores[]);
void calculateScore(int scores[], char grades[]);
void outputResults(string names[], int scores[], char grades[]);
	ifstream namesAndScores;

int main()
{
	string names[10];
	int scores[5];
	char grades[11];
	namesAndScores.open("out.txt");
	
while(!namesAndScores.eof())
          {
	namesAndScores >> names[0];
	namesAndScores >> scores[1];
	namesAndScores >> scores[2];
	namesAndScores >> scores[3];
	namesAndScores >> scores[4];
	namesAndScores >> scores[5];

// Name
	cout << names[0] << " ";
// Scores
	cout << scores[1] << " ";
	cout << scores[2] << " ";
	cout << scores[3] << " ";
	cout << scores[4] << " ";
	cout << scores[5] << " ";
    cout << endl;
}

}



If you want names[0]=Johnson and names[1]=Aniston and you don't want to use a loop just take out the while loop and read each line, assign all the values then work with those.


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

using namespace std;
void fillArray(string names[], int scores[]);
void calculateScore(int scores[], char grades[]);
void outputResults(string names[], int scores[], char grades[]);
	ifstream namesAndScores;

int main()
{
	string names[10];
	int scores[50];
	char grades[11];
	namesAndScores.open("out.txt");
	
// Line 1
	namesAndScores >> names[0];
	namesAndScores >> scores[1];
	namesAndScores >> scores[2];
	namesAndScores >> scores[3];
	namesAndScores >> scores[4];
	namesAndScores >> scores[5];
// Line 2 
	namesAndScores >> names[1];
	namesAndScores >> scores[6];
	namesAndScores >> scores[7];
	namesAndScores >> scores[8];
	namesAndScores >> scores[9];
	namesAndScores >> scores[10];
	
// Name
	cout << names[0] << " " << endl;
	cout << names[1] << " " << endl;
// Scores
cout << "Scores for "<< names[0] << " ";
	cout << scores[1] << " ";
	cout << scores[2] << " ";
	cout << scores[3] << " ";
	cout << scores[4] << " ";
	cout << scores[5] << " ";
    cout << endl;
cout << "Scores for "<< names[1] << " ";
	cout << scores[6] << " ";
	cout << scores[7] << " ";
	cout << scores[8] << " ";
	cout << scores[9] << " ";
	cout << scores[10] << " ";
    cout << endl;


}
I believe the OP is wanting to do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
 ifstream inFile("myFile.txt");
string myNames[20];
int temp;
for (int i = 0; i < 20; i ++) {
   inFile >> myNames[i];
   // Discards scores
   for (int j = 0; j < 5; j ++)
      inFile >> temp;
}

for (int i = 0; i < 20; i ++)
   cout << myNames[i] << "\n";


Granted, you're still using a loop (there are better, more efficient ways than what I did), but from what I understand is that you only want the names, you don't care about the scores. Yes, you can still do the above without loops of any type, but the issue comes in the form of not knowing when the end of the file is reached (something I left out in my loop) and manually having to type all of that out, which is really boring.

If this isn't what you were looking for, you might have to explain into more detail.
Topic archived. No new replies allowed.