I'm having trouble storing my code into two different arrays. The following text file contains a format like this: name,1,2,3,4,5anothername,6,7,8,9,10...
For example it could be something like this, I'll name it test.txt:
Drake,1,2,3,4,5
Kanye West,6,7,8,9,10
Ka,11,12,13,14,15,16
Young Thug,17,18,19,20
Kendrick Lamar,21,22,23,24,25
Here is my code so far:
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main(){
ifstream inputFile;
inputFile.open("test.txt");
if (inputFile.fail()){
cout << "File open error!" << endl;
return -1;
}
string namesarray[25]; //array used to store names
float numbersarray[25]; //array used to store numbers
string line; //string used to read text file
int i = 0; //counter for entire text input
int j = 0; //counter for namesarray
int k = 0; //counter for numbersarray
while(getline(inputFile, line, ',')){
if (line[i] >= 'A' && line[i] <= 'Z'){
namesarray[j] = line;
j++;
}
else{
numbersarray[k] = stof(line);
k++;
}
i++;
}
inputFile.close();
}
My problem is that I can't store all the names into the string array, but the numbers are stored into the float array just fine. My code only stores in the first name and then a random number. For example if I create a loop to check if the names or the numbers are stored in correctly
for (int a = 0; a < 25; a++){
cout << numbersarray[a] << endl;
}
The numbers are stored in fine but checking for the names it doesn't store all the names in there. If I'm checking for the first letter of the line shouldn't it store the name in there if it contains a letter? I don't want to use isalpha() because it still outputs the same problem.
for (int a = 0; a < 25; a++){
cout << namesarray[a] << endl;
}