Hello, I'm new to this forum and have a brief knowledge of C++. I made this program to ask the user for a word and make it so the word would not be allowed any "i's". I completed the task just my code looks a bit to much I think. Can you suggest ways i shorten it. Thank you.

// Code Shark

#include <iostream>

using namespace std;

int main() {
    char word[20];
    int i = 0;
    
    cout << "Enter a word which includes NO i's : ";
    cin >> word;
    
    do {
        word[i];
        
        if(word[i] == 'i') {
                   i++;
                   cout << endl << "I asked for no i's!";
                   cin.get();
                   cin.get();
        }
    }
    
    while(i);
    
    return 0;
        
    cin.get();
    cin.get();
}

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;
}

I guess it is C++ , you can use std::string apis like find , your code will reduce drastically.

commented: Was my though too :) +33

@AncientDragon
Doesn't the loop you give continually ask for strings until you enter one that doesn't have an 'i'? The original program just asked for one, and check if there was an 'i' in it, then ended. You can reduce lines 14-25 to

if( strchr(word,'i') )
   {
      cout << endl << "I asked for no i's!";
   }

You can also remove line 9, the deceleration of the variable i, if you employ this method.

Also, the program terminates after return 0; , so there's no point in the two cin.get() statements after that. They can either be removed, or, you can put them before return 0; if you want them to be executed.

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;

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;

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;

Wait, when will the loop terminate? And what does the && operator do with left-operand istream and right-operand size_t? Is that even defined?

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

#include <boost/regex.hpp>
#include <iostream>
#include<string>

int main()
{
  std::string word;
  boost::regex e = "\b.+?[^'].+\b";
  boost::smatch what;
  std::cout << "Enter a word";
  std::cin >> word;
  if(boost::regex_match(word, what, e)
  {
    std::cout << "The word can not include ' " << std::endl;
  }
  std::cout << "Thank you. Bye..." << std::endl;
}

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.

commented: Lol that gave me a laugh! +1

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...

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...

Um, so if I got that right, the loop will terminate when the input stream goes bad, or when the user inputs a string without an 'i'. This isn't what the OP's program did. His program only asked for a single string, checked if it had an 'i', gave a warning if it did, and terminated.

Sorry, but the original program has exactly the same effect: see do-while loop in the original post...

Sorry, but the original program has exactly the same effect: see do-while loop in the original post...

May be the original program is supposed to have the same effect

Sorry, but the original program has exactly the same effect: see do-while loop in the original post...

The do-while loop in the original post was being used to cycle through the letters of the string to see if any of them was an 'i'. The cin >> word; statement was executed before the loop began, and will only be executed once.

The do-while loop in the original post was being used to cycle through the letters of the string to see if any of them was an 'i'.

And it does? If the first letter is 'i' No way the program ends. If the first letter is not 'i' even if 'i' is present somewhere else in the string it will just ignore it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.