I have a file that has record of how many people picked specific color out of all people in different classrooms. I am trying to read that file into a array based on the color. for example if the color is Red I want to save the 2 numbers from that line into an array that keeps list of red numbers.The file is already formatted this way and cannot be changed.The first problem I encountered is that string is not read from file properly.
Here is whats inside of the file. There were other colors as well I decided to only include them once the program runs properly
Red 18 20
Black 15 20
Red 13 18
Black 12 16
Here is what I have tried
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string myString = " ";
int redArray[4]= {0};
int blackArray[4] ={0};
int counter = 0;
std::ifstream myFile;
myFile.open("record.txt");
while (!myFile.eof())
{
getline(myFile, myString, ' '); //Get the first text and ignore spaces
//Runs this block if the color is red
if (myString == "Red")
{
myFile >>redArray[counter]; //Get the numbers from the red rows
}
//Run this block if the color Black
else if (myString == "Black")
{
myFile >>blackArray[counter]; //Get the number from black rows
}
counter++;
}
cout <<myString <<endl; //to show if string is read properly
//Display the values of redArray
for (int i = 0; i <4; i++)
{
cout <<redArray <<" ";
}
cout <<endl;
//Display the values of blackArrays
for (int k = 0; k <4; k++)
{
cout <<blackArray[k] <<" ";
}
return 0;
}