Help Two Dimensional Array File Read
Nov 26, 2011 at 12:44am UTC
Can someone tell me why my input file will not read to my two-dimensional dynamic array?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
int main()
{
ifstream inFile("Hey.txt" );
inFile.precision(2);
inFile.setf(ios::fixed, ios::showpoint);
const int rows = 10;
const int cols = 10;
string** memberID;
memberID = new string*[rows];
for (int i = 0; i < rows; i++)
{
memberID[i] = new string[cols];
inFile >> memberID[i];
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
cout << memberID[i];
}
}
inFile.close();
system("PAUSE" );
return 0;
}
Nov 26, 2011 at 1:05am UTC
Get rid of line 13.
It should be at line 19.
If you also want to print what you read you can have lines 13 and 19 together, or create another nested loop for display after line 22.
Hope this helps.
Nov 26, 2011 at 1:06am UTC
inFile >> memberID[i];
memberID[i] is a string* so that will not work. You probably want to put an extra loop here to read in all the strings
1 2 3 4
for (int j = 0; j < cols; j++)
{
inFile >> memberID[i][j];
}
Also in your output loop you probably want to use cols on the inner loop condition.
Nov 26, 2011 at 1:14am UTC
Thanks for the replies.
I took both of your advice and created this loop:
1 2 3 4 5 6 7
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
inFile >> memberID[i][j];
}
}
However, I still get the message "No operands match '>>' these operands".
Any suggestions?
Nov 26, 2011 at 1:19am UTC
It should work
Nov 26, 2011 at 1:37am UTC
I found the problem. Thanks for the help.
It should but it keeps giving me that message. Maybe you can help me out with the single dynamic array version I made though. In this code, I'm able to read out from my text file, but I would like to read out only the first name of the member ID at the end of the file that matches.
Here is my text file:
3
Titanic
Kate Winslet
Leonardo Dicaprio
Cameron
cameron
20th Century Fox
2
Donald Duck 1
Mickey Mouse 2
Minnie Mouse 3
Goofy Dog 4
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string targetID, totalMovies, movieT,fname, lname, memID;
cout <<"Enter member ID: " ;
getline(cin,targetID);
string* memberID;
ifstream memberFile;
memberFile.open("Hey.txt" );
memberID = new string[20];
for (int i =0; i < 20; i++)
{
string fname, lname, memID;
memberFile >> memberID[i];
memberID[i] = fname +" " + lname +" " + memID;
}
for (int i = 0; i < 20; i++)
{
if ((targetID == memberID[i]))
{
cout << "Welcome " << memberID[i-2] << " " ;
}
}
system("PAUSE" );
}
Any suggestions?
Last edited on Nov 26, 2011 at 2:12am UTC
Topic archived. No new replies allowed.