I'm trying to open a file, read some data out of it into an array, and then display those values to the screen and then hopefully I can edit them, manipulate them, and then write them back to the same file.
Is an array a good way of going about this? I'm pretty clueless on where to start for something like this. Any examples would be greatly appreciated, or any links to some information.
If it helps, the information that I'm trying to read from a file is going to be a mix between text and numbers. I'm trying to open a file full of description names and grades. For example:
edit: just relized you said read from file, the above idea could work with arrays or vector but just switch the cins for the proper file reading, it just depends on how the txt file is formated
something like that (sorry not at my home comp, cant test it and its late) but the idea should work sry cant help more, have to goto bed
Arrays only if you know the greatest possible number of grades at compile time because arrays are fixed in length in C++. (Dumb, I know, but thats the way it is.)
Pointers are a little advanced, but not too hard once you get the swing of things.
I agree with DrolArumil that vectors are probably your best bet.
I'm kind of hoping to use arrays, because in my C++ class, we haven't gone over Vectors or Pointers yet. So all the above code kind of looks like gibberish to me.
Well a vector, the way I used it is basicly the same as an array. The key difference is that a vector is easiliy resizeable and cleans up after it self (no need for new/delete to change array size or allocate new array)
But as long as you know how big the array will be just declare it ahead of time
to read into an array is the same as a normal variable so lets say I want to populate the array from a file (asume everything has been declared properly) it depends on if you want to read a line, or whole file, or comma delimited. lets say its comma delim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//asume the file looks like this
//Item Description Points Score,Item Description Points Score,
//etcetcetc......
for (int i = 0; i < ARRAY_SIZE; i++)
{
geline(inFile, anArray[i], ',');
}
//So lets see what we got
for (int j = 0; j < ARRAY_SIZE; j++)
{
cout << anArray[i] << endl;
}
this will populate your array and display it to the console, or you can use multiple arrays..or w/e u want.