I need to read data from a file given to me by using an array.
Data in file looks like this: "Jones C A B B D A B C A A C B A D D C A C A D
I'm making an Exam Grader program but I am not sure how to skip the last name and just read the answers.
I need to read data from a file given to me by using an array.
Data in file looks like this: "Jones C A B B D A B C A A C B A D D C A C A D
I'm making an Exam Grader program but I am not sure how to skip the last name and just read the answers.
Here's what I'm thinking: Hope it helps!
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void){
String learnerName; //learner name stored in the string
char grades[] = {\0}; //char array to read grades
ifstream fromFile("Filename.txt", ios::in); //stream to open and read file
if(fromFile.fail()){ //check if file was succesfully opened
cerr << "***Could not open file!***" << endl;
exit(-1);
}
fromFile >> learnerName; //get name of the leaner
int counter = 0;
while(!fromFile.eof()){ //read the grades after the learner name
fromFile >> grades[counter]; //continue from after the last name
counter++;
}
int array_length = sizeof(grades) / sizeof(grades[0]); //find length of array
for(int i = 0; i < ){ //print out grades of learner + name
cout << "Name: " << learnerName << endl;
cout << grades[i] << " ";
}
return 0;
} //end main(void);
Your problem is that
char grades[]
donĀ“t have the required space of memory.
Try something like:
char grades[100];
and be sure that
counter
is alway below 99.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.