So I want to take in information from a file, it will start with names(strings) and will eventually change to integers.
Ex. for nums.txt
James Smith
John Jones
Amy Li
1 3 2 3
3 2 4 1 0
I want to write a program that stores each name (one name per line) and then when names end and numbers begin, it starts adding each # occurrence to an array. I.E. if 3 2's appear, i want
numInt[2] to equal 3
I want to do this using an ifstream to take input from a file, and use a stringstream to sort strings and integers. so far I have this
int main() {
string names[10];
int numNames = 0;
int numInt[100] = {};
ifstream file("nums.txt");
stringstream ss;
string s;
int n;
while (file >> ss) {
while (ss >> s) {
names[numNames] = s;
numNames++;
}
while (ss >> n) {
numInt[n]++;
}
}
return 0;
}
I know I'm going about this wrong but I'm not sure how to properly do this.
I think I'd do something like this:
This supports what you've asked for, but doesn't strictly enforce it. IOW, if you had something like:
...it would put "Joe Blow" and "Jerry Coffin" in the
names
and1
,2
and3
in thenumbers
.