#include <iostream>
#include <fstream>
usingnamespace std;
ifstream inStream;
ofstream outStream;
int main()
{
inStream.open("input4.txt");
if (inStream.fail()){
cout<<"Unable to open file!"<<endl; }
else {
int i = 1;
char word[200];
while(!inStream.eof()){
inStream>>word;
cout<<i<<". "<<word<<endl;
i++;}}
system("Pause");
return 0;
}
and the output would be this:
1. I
2. like
3. bananas.
I know I don't do anything to change it, which is why I'm asking, because I don't know how to count punctuation as a word using Char or string. :\ .
Any suggestions?
To be, or not to be, that is the question:
Whether 'tis Nobler in the mind to suffer
The Slings and Arrows of outrageous Fortune,
Or to take Arms against a Sea of troubles,
And by opposing end them: to die, to sleep.
Read a whitespace delimited token and check if the token is just a word or a word with punctuation as a suffix. If it is a word with punctuation, split it into two - a word and punctuation. (Hopefully, words in the text would not be split across lines with hyphenation.)
Token: To -> word To
Token: be, -> word with punctuation. word be, punctuation ,
Token: question: -> word with punctuation. word question, punctuation :
Token: outrageous -> word outrageous
Token: 'tis -> word 'tis
> Because my dad said I have to use a character array
Well, I assumed that you are just beginning to learn C++; the point I was trying to make, based on that assumption, is this:
Even for the professional programmer, it is impossible to first learn a whole programming language and then try to use it. A programming language is learned in part by trying out its facilities for small examples. Consequently, we always learn a language by mastering a series of subsets. The real question is not ‘‘Should I learn a subset first?’’ but ‘‘Which subset should I learn first?’’
One conventional answer to the question ‘‘Which subset of C++ should I learn first?’’ is ‘‘The C subset of C++.’’ In my considered opinion, that’s not a good answer. The C-first approach leads to an early focus on low-level details. It also obscures programming style and design issues by forcing the student to face many technical difficulties to express anything interesting. ...
C++’s better support of libraries, better notational support, and better type checking are decisive against a ‘‘C first’’ approach. However, note that my suggested alternative isn’t ‘‘Pure Object-Oriented Programming first.’’ I consider that the other extreme.
To take an example that illustrates the issue (lifted almost verbatim from the above paper), consider a simple programming problem: read the next white-space delimited token from input.
Robust code using std::string:
1 2 3
std::string token ;
std::cin >> token ;
std::cout << "the token is: " << token << '\n' ;