here is the pesudo file.
REREREEEEEERRRREER
SOMEStrinG1234 RERRRR EEERRRREER
SOMEStrinG1224 REREREEEREERRR REE
SOMEStrinG1214 REREREREREREREEEER
SOMEStrinG1204 RERE EEEEEERRRRRRR
SOMEStrinG1294 REREEREEE ERRRREER
SOMEStrinG1284 REREREEEEEERR REER
here is my attempted code
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#define SIZE 255
using namespace std;
int main(int argc, const char * argv[])
{
ifstream inFile;
inFile.open("inFile", ifstream::in);
if (!inFile) {
cerr << "Error: file could not be opened" << endl;
return -1;
}
// reading file line by line // EOF = no chars;
string line;
int i = 0;
while (inFile.good()) {
getline(inFile, line, '\n');
cout << line << " line// " << i << endl;
i++;
}
cin.clear();
char studentID;
string grades;
for (int i = 1, k = 0; line.length(); i++, k++) {
cin >> studentID[k] >> grades[k];
}
cout << studentID[2] << endl << grades[2][1];
ofstream outFile;
outFile.open("outData.txt", ifstream::out);
if (!outFile) {
cerr << "Error: file could not be opened" << endl;
return -2;
}
return 0;
}
The context is this is for an exercise out book I am attempting to solve.
the point I am at in my coding is i would like to test my results by adding this statement inline.
cout << studentID[2] << endl << grades[2][1];
But it is not outputting anything to common out.
The section I am working in is about c++ arrays (non vector)
So that is the scope of solution(s) feedback I am looking for
Thanks in advance.
Take a close look at this:
cin >> studentID[k]
if studentID is a 2d array of characters, what do you get by specifying both indexes? A single character, which is what you end up reading.
If you want to read a string, try
cin >> studentID[k]
But you're going to run into problems with garbage stuck in your input because cin hits a newline and refuses to read past it. Are you allowed to use C-style fgets/sscanf ?
"But you're going to run into problems with garbage stuck in your input because cin hits a newline and refuses to read past it. Are you allowed to use C-style fgets/sscanf ? "
No requirements / limits here other than I learn what I am doing. It is not for a class. Just an exercise out of a book. I would just like to avoid using vectors. As I am still trying to work with basic arrays for now.
I keep noticing more problems with your strings... string[SIZE] is an array of 256 strings, not 256 characters!
I would use C I/O, then. It's easier and more efficient, and deals with straightforward arrays.
We use fgets and sscanf, rather than plain scanf, because plain scanf is subject to the same problems -- it'll get stuck on a newline until you flush input or whatever. Read entire lines at a time then process them, and nothing gets stuck.
#include <stdio.h>
// 2D character arrays, each row holds up to 64 characters
char studentID[256][64];
char grades[256][64];
int main(void)
{
int n=0;
char firstline[256], buf[256];
fgets(firstline, 256, stdin); // Read a line of up to 256 characters into firstline
// read a line into buf, then split it into studentid and grades
while(fgets(buf, 256, stdin) != NULL)
{
// The string "%s %s" means "two strings separated by a space"
// It expects two pointers afterwards, one for each %s
// studentID[n] is a pointer to a 1d array inside the 2d one.
sscanf(buf, "%s %s", studentID[n], grades[n]);
n++;
}
}