0

I can't seem to get my program to read all the numbers seperated by commas in my csv file. It outputs only one number.

I've looked everywhere and I can't get it to work with the csv file.

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

using namespace std;


int main() {
    ifstream inputFile;
    int number;

    // open the file //
    inputFile.open("salesData.txt");

    // reads the numbers from the file and displays them //
    while (inputFile >> number)
    {
        cout << number << endl;
    }


    system("pause");

    return 0;
}

The program outputs

5

I need it to putput

 5 453.67 8769.57 221.87 600.28 8123.00
R Sahu
  • 204,454
  • 14
  • 159
  • 270
CAP_BLUE
  • 1
  • 1
  • 1
    You need to skip the comma, before reading the next number. Otherwise the stream sees a comma while trying to read the number and enters an error state – Michael Veksler Feb 19 '19 at 04:48
  • 1
    Also, don't use type `int` if you want to keep decimals – donpsabance Feb 19 '19 at 04:49
  • 1
    This answer https://stackoverflow.com/a/7868998/9176689 might help you. There is an example for parsing csv in the comments of it, too. – nick Feb 19 '19 at 04:50

0 Answers0