Hey guys I have been trying to get make a simple program to store all of the names, in a file, in an array of strings. But I'm not quite sure how. I got to this till I couldn't figure it out any further. Is the code below on the right track, and if so, why am I getting the error?
Code:
// reading a text file
#include <iostream> //import
#include <fstream>
#include <string>
using namespace std; //namespace std
char names[25][10]; //make an array of 25 strings, 10 chars long
int i = 0;
int main () {
string line; //initiate a string
ofstream people ("example.txt"); //open file
people<< "Joe \nBob \nBill"; //write names
people.close(); ///close file, so it can be opened in read mode
ifstream myfile ("example.txt"); //read file
if (myfile.is_open()) //check if file is there
{
while (! myfile.eof() ) // set up loop
{
getline (myfile,line); //read line
names[i] = line; //put string in array<!--gets error-->
i++; //sets array position
}
i = 0;
while(i < 3)
{
cout<< names[i]; //reads array of strings
i++;
}
myfile.close(); //closes file
}
else cout << "Unable to open file"; //If file is not there
return 0;
}
Which gives the error
Code:
main.cpp: In function `int main()':
main.cpp:30: error: incompatible types in assignment of `std::string' to `char*[50]'
main.cpp:49:2: warning: no newline at end of file
Process terminated with status 1 (0 minutes, 0 seconds)
Why is it not allowing me to enter the string into the array?
Thanks for any help,
Joe