0

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;
}






  • ```getline``` reads the whole line into the string, so if you test ```myString == "Red"```, it will always evaluate to ```false```, even if the line starts with "Red". Also, you don't need to initialise ```myString```. It is in a well-defined state without it. – RL-S Apr 02 '20 at 13:39
  • 1
    Mandator reads: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) and [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). – molbdnilo Apr 02 '20 at 13:41
  • 1
    Please be more specific than "is not read properly". How exactly *is* it read? – molbdnilo Apr 02 '20 at 13:42
  • I normally use a ```std::stringstream``` as a converter, then copy the string that I got from ```getline``` into it, like ```converter.str(myString);```. I'd then define tokens for what I'm looking for, such as ```std::string colour;``` and then fill it with ```converter >> colour```. NOW you can test for ```colour == "Red"```. – RL-S Apr 02 '20 at 13:45
  • 1
    There are two number for each colour, but you only read one of them. Print what you've read, and you will see that your second colour is "20". – molbdnilo Apr 02 '20 at 13:50
  • 1
    @RL-S With `getline(myFile, myString, ' ')` (please note that the space is set as delimiter), after the first read `myString` should contain in fact `"Red"`. From [getline()](https://en.cppreference.com/w/cpp/string/basic_string/getline): _the next available input character is delim, as tested by Traits::eq(c, delim), in which case the delimiter character is extracted from input, but is not appended to str._ Though, a `myFile >> myString` should have done as well. – Scheff's Cat Apr 02 '20 at 13:56
  • I think you should make a counter for each array. For example during the first iteration of the while, the counter is 0 and the first line is read and processed, then the counter is incremented to 1, and then the next iteration. By the time some if statements prove true there will be no way to read into the arrays using a previous counter. And probably you arrays should be arrays of arrays to contain the two ints for each color? – octopus Apr 02 '20 at 14:55
  • @RL-S I have set the delimiter as a space shouldn't it only grab the first text up to a space? –  Apr 02 '20 at 15:54

0 Answers0