1

I have a file in the format: FirstName,MiddleName,LastName,Major,City,State,GPA

I'm trying to read in the file and output the data without the commas to the screen. This is what I have so far, but it only outputs the GPA's:

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

using namespace std;
int main(){
    string fileline;
    string word;
    ifstream studentData;
    studentData.open("studentData.csv");
    while(studentData){
        getline(studentData,fileline);
        istringstream ss(fileline);
        while(getline(ss, word,','));{
            cout << word << '\n';

        }

    }
    return(0);
}
Jordan C
  • 11
  • 1
  • 2

3 Answers3

1

I think the problem is this line:

while(getline(ss, word,','));{

Try removing the semicolon. This would be the right way:

while(getline(ss, word,',')){

The semicolon makes the loop do nothing until it reads the last word (which I'm guessing is the GPA), which you then print.

Let us know if that works!

  • That did the trick. Currently it prints out each word on its own line. I'm trying to figure out how to only print to a new line once i reach the end of a line, so it would print 7 words then start a new line. Any tips? – Jordan C Nov 06 '15 at 00:45
  • Yes. It's printing a new line because of the \n at the end of cout. To print with spaces you can do something like this: while(getline(ss, word,',')){ cout << word << " "; //Print a space instead of a new line } cout << "\n"; //And only then print the end of the line – Guilherme Sena Nov 06 '15 at 00:46
  • I want to output the data in the same form that it starts in, just without commas. If i do ' ' instead of '\n' it will put every word it finds next to one another. (I should have mentioned that the input file has around 27 lines). so I want to print out 7 words then new line rinse and repeat – Jordan C Nov 06 '15 at 00:51
0

If you just want to copy without commas, why not just copy without commas?

ifstream studentData("StudentData.csv");

studentData >> noskipws;

std::remove_copy(std::istream_iterator<char>(studentData),
                 std::istream_iterator<char>(), 
                 std::ostream_iterator(std::cout)
                 ',');

OTOH, if your input doesn't have anything separating the fields except the commas, this will produce output with them all run together. You might prefer to write out a space character between fields instead:

std::transform(std::istream_iterator<char>(studentData),
               std::istream_iterator<char>(),
               std::ostream_iterator<char>(std::cout),
               [](char ch) { return ch == ',' ? ' ' : ch; });

This way you'll still have something to help keep track of where one field ends and the next starts. Of course, if you prefer, say, a tab rather than a space, you can change the space to a tab.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

The semicolon in while(getline(ss, word, ','); shouldn't be there. That said, stringstreams are slow, I'd prefer to do this as follows.

while(std::getline(studentData, fileline)) {
    std::erase(std::remove(fileline.begin(), fileline.end(), ','), fileline.end());
    std::cout << fileline << '\n';
}
Aakash Jain
  • 1,963
  • 17
  • 29