0

so the task for this code is to copy students' names and grades from an csv excel sheet into xcode and then put them into arrays and put them into a new excel sheet. The problem that i seem to be having is that the getline does not go to the next line. To make sure that there wasn't an error somewhere in this code that would cause that to happen, I wrote a very small and completely different program to see how getline works and found that it does not skip to the next line. In fact, if I raise the character amount to a high number, it will just copy in the entire excel info into the array. Here's my code:

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>



using namespace std;

char line[80];
string students[100];
int grades[50][20];
char *p;
int r;
int q;


void read_sheet();
void print_sheet();

int main() {

read_sheet();

print_sheet();

return 0;
}

void read_sheet(){

ifstream file1("/Users/JohnnyD/Downloads/Project_MAC101.csv");


file1.getline(line, 79); // this puts everything from the first line of the
                        // excel sheet into the array line


for(r=0;r<50||(!file1.eof());r++){ // this is a loop that goes up to
                                   // either the amount of students
                                  //(50 is max) or the end of the

    file1.getline(line, 79);    // this is suppose to put everything
                                //from the second line into the array
                                // line, but I don't think it is doing
                                // that.

    p=strtok(line,",");         // this takes everything from the first
                                // line that is before the comma and
                                //puts it into p.(should be only a single
                                // student's name
    students[r]=p;              // this puts the name into the array
                                // called students

    cout << students<<endl;    // this is only a test to see if the names
                               // are going properly to the array. I
                                // wouldn't normally have this in the code.
                                // This is where I found out that it's not
                                // skipping to the next line because the
                                // output just spits out "name" over and
                                // over again which means that it never got
                                // passed the first word in the excel sheet.
                                // ("name" is the first word in the first
                                // line in the excel sheet)

    for(q=0;q<20;q++){          // this is a loop that goes to the end of
                                // the column where 20 is the max amount
                                // of grades

        p=strtok(NULL,",");     // puts each grade before the comma into p.
        if(p==NULL)             // if it's the end of the line, break out
          break;                      //of the loop.

        grades[r][q]=atoi(p);   // this changes the string to integer and then
                                // puts it into the array grades


    }

 }


file1.close();
}



void print_sheet(){

ofstream file2("testing.csv");

for(int y=0;y<=r;y++){

    file2<<students[y];
    for(int h=0;h<q;h++){

        file2<<grades[y][h];
    }
    file2<<endl;
}

file2.close();
}

This is the code that I used to test to see if getline was actually moving to the next line.

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
char line[80];

int main() {

ifstream file1("/Users/JohnnyD/Downloads/Project_MAC101.csv");

file1.getline(line, 79);
cout << line<<endl;

file1.getline(line, 79); // shouldn't this then go to the next line?
cout << line<<endl;       // It doesn't. It only repeats the first getline

return 0;
}
JohnnyD27
  • 73
  • 1
  • 7
  • 2
    `(!file1.eof())` is not a good loop condition. [c++ - Why is iostream::eof inside a loop condition considered wrong? - Stack Overflow](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – MikeCAT Jun 09 '16 at 13:27
  • I'm surprised that `cout << students< – molbdnilo Jun 09 '16 at 13:39
  • Do you really want to continue reading from `file1` after eof just because `r` hasn't reached 50? I think you meant to use `&&`. – starturtle Jun 09 '16 at 13:44

1 Answers1

0

The usual idiom for reading from a file is to use a while statement.

In your case, you can limit it with another variable:

const unsigned int maximum_records = 50U;
unsigned int record_count = 0U;
std::string text_line;
// ...
while (getline(datafile, text_line) && (record_count < maximum_records))
{
  //...
  ++record_count;
}

If either the file operation failed or the maximum records have been reached, the input session will terminate.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144