Need help regarding reading data from a text file.

closed account (S37X216C)
I have a project that requires to create 1D arrays to store names and ids. A 2D array to store ages. Then we have to read the data into arrays from a text file, if the file isn't found we need to include a error message.
There is a delimiter which is '|'.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream inFile;
const int INFO = 5;
string fullName[INFO];
int fullIds[INFO];
const int ROWS = 5;
const int COLS = 4;
int ages[ROWS][COLS];

inFile.open("info.txt");
if (inFile)
for (int a = 0; a < INFO; a++){
getline(inFile, fullName[a], '|');
inFile >> fullName[a];
cout << fullName[a] << endl;
}
else{
cout << "Error" << endl;
return 0;
}

So far I have this only for the string name but I'm not sure what I'm doing wrong in this part.
Last edited on
What is your file's content?
closed account (S37X216C)
It's id, name, and then age. It has int and strings.
So it looks like this,
7777, Aven, 10
8888, Avery, 12
etc.

For your code, please use code tags (<> icon at the right of the text box)

If the delimiter is '|' how come it's not at the end of each line on your file?
Shouldn't you use ',' as a delimiter instead?
What's the purpose of storing ages in a 2d array, what age do you put in which column?

You're missing a curly bracket after your if statement, before your for loop.

If you've got things written on your file in that order, you should read and store information in the order they appear on the file.
Topic archived. No new replies allowed.