-1

I have a CSV file that has statistics from Johns Hopkins about COVID. I have been given an assignment to take the information and do several tasks to it. I have successfully pulled the csv into the program but the formatting is no good.

This is the current format it outputs.

I would like to output it in a style more suited to the excel layout it comes from.

This is the Source format

What is the best way to achieve this? Im new to working with c++ and .csv.

  • 2
    R or Python are much more suitable to do analysis work on data sets. Your question is too broad. What exactly is it that you want to do? If you are asking how to parse CSV files, then you should look for a library. There is nothing by-default in C++ that does this for you. But library recommendations are off-topic here. For simple parsing, see [How can I read and parse CSV files in C++?](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c). – walnut Mar 28 '20 at 16:42
  • It looks as if the data is parsed using space as separator. Try with `,` instead. – Danny_ds Mar 28 '20 at 16:44
  • 2
    All questions here must have all relevant information ***in the question itself as plain text***. Links can stop working at any time making questions meaningless. Code, data, or errors, shown as images cannot be copy/pasted; or edited or compiled for further research and investigation. This question must be [edit]ed, and all links and images removed and replaced with all relevant information, as plain text. All code must meet all requirements of a [mre]. You can find many other questions here that explain everything in plain text. There's no reason why this one can't, either. – Sam Varshavchik Mar 28 '20 at 16:44

1 Answers1

0

Please, find a cure for COVID now ;^)

/**
  g++ -std=c++17 -o prog_cpp prog_cpp.cpp \
      -pedantic -Wall -Wextra -Wconversion -Wno-sign-conversion \
      -g -O0 -UNDEBUG -fsanitize=address,undefined
**/

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

std::vector<std::vector<std::string>>
read_document(const std::string &file_name)
{
  auto content=std::vector<std::vector<std::string>>{};
  auto row=std::vector<std::string>{};
  auto field=std::string{};
  auto input=std::ifstream{file_name};
  auto c=char{};
  while(input.get(c))
  {
    if(c=='\n')
    {
      row.emplace_back(std::move(field));
      field.clear();
      content.emplace_back(std::move(row));
      row.clear();
    }
    else if(c==',')
    {
      row.emplace_back(std::move(field));
      field.clear();
    }
    else
    {
      field+=c;
    }
  }
  if(!empty(field))
  {
    row.emplace_back(std::move(field));
  }
  if(!empty(row))
  {
    content.emplace_back(std::move(row));
  }
  return content;
}

int
main()
{
  const auto content=read_document("my_file.csv");
  for(const auto &row: content)
  {
    for(const auto &field: row)
    {
      std::cout << '|' << field;
    }
    std::cout << "|\n";
  }
  return 0;
}
prog-fh
  • 13,492
  • 1
  • 15
  • 30