line 15: does nothing so you might as well delete it.
The loop lines 14-25 can be rewritten like this:
char word[20];
while(1)
{
cout << "Enter a word which includes NO i's : ";
cin >> word;
if( strchr(word,'i') )
{
cout << endl << "I asked for no i's!";
}
else
break;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I guess it is C++ , you can use std::string apis like find , your code will reduce drastically.
ithelp
Nearly a Posting Maven
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
Never never never use cin >> char array!
string word;
while (cout << "Enter a word which includes NO i's : ",
cin >> word,
word.find('i') != string::npos
)
cout << "I asked for no i's!" << endl;
cout << "Thank you. Bye..." << endl;
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Sorry, more robust code (Ctrl-Z reaction added):
string word;
while (cout << "Enter a word which includes NO i's : ",
cin >> word &&
word.find_first_of('iI') != string::npos
)
cout << "I asked for no i's!" << endl;
cout << "Thank you. Bye..." << endl;string word;
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
OK I tink if code pass all unit tests it's good code. Your code is good, but use std::string not char array with cin >>. If you want to catch all words that have ' , you can use boost regex lib http://www.boost.org/doc/libs/1_35_0/libs/regex/doc/html/index.html
I think you have over complicated things for this assignment. All the op wants is for the program to detect when the word includes the letter 'i'. Doing regular expressions if far far beyond the scope of this assignment.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Some explanations:
Stream operator >> (and << too) returns reference to stream (that's why we may couple expressions in
cin >> first >> second;
// (cin >> first) >> second - again stream >> target
Stream classes have conversion to bool (inherited from the common ancestor). It returns true if stream is OK otherwise false). Operator && is a simple C logical AND.
So the loop will been terminated when:
cin returns false (end of input stream or i/o error)
AND
find_first_of() returns std::string::npos value (no i's in the world).
The while stmt condition follows by the template:
Do_prompt , input_OK AND word_with_i
Do_prompt is the 1st arg of a comma operator (do this and forget then do the 2nd arg).
Some correction of my prev post:
1. Of course, the literal must "iI", not 'iI'
2. Discard string word in the tail (careless copy/paste op artifact;)
Sorry if this post is duplicated: I have some Inet troubles now...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Sorry, but the original program has exactly the same effect: see do-while loop in the original post...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348