I have this simple program. I created a file called input.txt containing a list of names and I want to read from it. I have two versions of the program. The first one only gives me the first letter in the file and nothing else. The second one does not work, but this is becuase I am not certain how to read from a file using strings. I know it in java but not in C++. Any help is apppreciated by this rookie programmer.
GCard
#include <fstream>
#include <iostream>
using namespace std;
class myfirstclass
{
private:
char msg[21];
int loopcounter;
ifstream myfile;
public:
void greeting();
myfirstclass();
};
myfirstclass::myfirstclass()
{
//open the file with the data
myfile.open("input.txt",ios::in);
myfile.getline(msg,21);
}
void myfirstclass::greeting()
{
cout << msg << "\n";
}
int main ()
{
myfirstclass myfirstobject;
myfirstobject.greeting();
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n') ;
return 0;
}
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
class myfirstclass
{
private:
string msg;
int loopcounter;
fstream myfile;
public:
void greeting();
myfirstclass();
};
myfirstclass::myfirstclass()
{
//open the file with the data
myfile.open("input.txt",ios::in);
myfile.getline(msg, 40);
//myfile.getline(msg,20);
}
void myfirstclass::greeting()
{
cout << msg << "\n";
}
int main ()
{
myfirstclass myfirstobject;
myfirstobject.greeting();
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n') ;
return 0;
}