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;
}