1

My specific program basically sparses through a csv file and asks for user input in the form of whether they would like to know how many times goods where delivered by Bike, Car, or Foot, all of this information is stored in the file being read. My issue is I don't know how to build a counter for the code so like when it sparse through the file looking for a string that matches the user input (Bike, Car, or Foot) and then counts all occurences and returns that value to display to the user. Here is my counter method right now that only returns zero and I have no clue what the logic i should use here is. I also think it is important to mention that p is given by a getvariable method so p equals the user input

int transport::counttimes(string p)
{
  ifstream inFile;
  inFile.open("donationDataFixed.csv");
  int c=0;
  string s;
  string piece;
  while(s!=p)
  {
    stringstream data(s);
    getline(data,piece,',');
    getline(data,piece,',');
    getline(data,piece,',');
    getline(data,piece,',');
    getline(data,piece,',');
    getline(data,piece,',');
    getline(data,piece,',');//transportation mode
    if (s==p)
    {
      c=c+1;
    }
  }
  return c;
}
Sungguk Lim
  • 6,109
  • 7
  • 43
  • 63
Puddle
  • 11
  • 2

2 Answers2

0

If that is your entire source, then I can tell you that you never assign "s" a string value.

Since "s" never has a string value, your counter never increments, hence you will always return 0.

Also you don't have logic to parse the entire file. There are several ways to do this. You can look at this post for looping the entire file.

How To Parse String File Txt Into Array With C++

user1050632
  • 680
  • 4
  • 13
  • 26
0

There are a plenty of ways parsing csv files. Your choice may depends on the libraries you are using/want to use (std, boost, ...)

A good post with pros and cons and several examples can be found here

Community
  • 1
  • 1
550
  • 1,070
  • 1
  • 13
  • 28