954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Program works but shorter code?

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();
}
Code Shark
Newbie Poster
14 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
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
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

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

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

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
 

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?

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

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;
}
galin
Newbie Poster
10 posts since Aug 2007
Reputation Points: 10
Solved Threads: 2
 
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
Team Colleague
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
 

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.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

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
 
Sorry, but the original program has exactly the same effect: see do-while loop in the original post...


May be the original program issupposed to have the same effect

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 
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.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 
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.

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You