Hello all this is my first post here :-). I'm having trouble in a C++ program that I'm trying to write i have a text file with two lines in it that i've made up here are the following two lines.
The dog goes bark.
The cat goes meow.
These two lines are in a txt file called test.txt.
Here is the following code for this program that I cann't get to work.
When i try to print the file it only prints out "goes bark and the full sentance the cat goes moew. I have spent countless hours on this program this weekend and I haven't figured one thing out about it. Please Help me..
Code:
#include <iostream.h>
#include <fstream.h>
#include "tstring.h"
class WordCount
{
private:
String word;
ifstream infile;
int count;
char c;
public:
int ReadStr();
int WriteStr();
void CountWords();
};
int WordCount::ReadStr()
{
infile.open("test.txt");
infile >> word;
return 0;
}
int WordCount::WriteStr()
{
infile >> word;
while(getline(infile, word, '\n'))
{
infile >> word;
}
cout << word << endl;
return 0;
}
void WordCount::CountWords()
{
count= count+1;
cout << "The number of words in the file is: " << count << endl;
}
int main()
{
WordCount WC;
char res;
do
{
cout << "1 Read String." << endl;
cout << "2 Print String." << endl;
cout << "3 Count Words." <<endl;
cout << "4 Exit." << endl;
cout << "Please Select an operation: " << endl;
cin >> res;
switch(res)
{
case '1':
WC.ReadStr();
break;
case '2':
WC.WriteStr();
break;
case '3':
WC.CountWords();
break;
case '4':
cout << "Thanks for using my program." << endl;
exit(1);
break;
default:
cout << "Invaild Option... Please Try again " << endl;
}
} while(res!='4');
}
Any type of help would be appreciated.
Thanks,
Achilles