943,829 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1611
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 13th, 2008
0

Program works but shorter code?

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. // Code Shark
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. char word[20];
  9. int i = 0;
  10.  
  11. cout << "Enter a word which includes NO i's : ";
  12. cin >> word;
  13.  
  14. do {
  15. word[i];
  16.  
  17. if(word[i] == 'i') {
  18. i++;
  19. cout << endl << "I asked for no i's!";
  20. cin.get();
  21. cin.get();
  22. }
  23. }
  24.  
  25. while(i);
  26.  
  27. return 0;
  28.  
  29. cin.get();
  30. cin.get();
  31. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Code Shark is offline Offline
14 posts
since Jul 2008
Jul 13th, 2008
0

Re: Program works but shorter code?

line 15: does nothing so you might as well delete it.
The loop lines 14-25 can be rewritten like this:
C++ Syntax (Toggle Plain Text)
  1. char word[20];
  2. while(1)
  3. {
  4. cout << "Enter a word which includes NO i's : ";
  5. cin >> word;
  6.  
  7. if( strchr(word,'i') )
  8. {
  9. cout << endl << "I asked for no i's!";
  10. }
  11. else
  12. break;
  13. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 13th, 2008
1

Re: Program works but shorter code?

I guess it is C++ , you can use std::string apis like find , your code will reduce drastically.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Jul 13th, 2008
0

Re: Program works but shorter code?

@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
C++ Syntax (Toggle Plain Text)
  1. if( strchr(word,'i') )
  2. {
  3. cout << endl << "I asked for no i's!";
  4. }
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.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 13th, 2008
0

Re: Program works but shorter code?

Never never never use cin >> char array!
C++ Syntax (Toggle Plain Text)
  1. string word;
  2.  
  3. while (cout << "Enter a word which includes NO i's : ",
  4. cin >> word,
  5. word.find('i') != string::npos
  6. )
  7. cout << "I asked for no i's!" << endl;
  8. cout << "Thank you. Bye..." << endl;
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jul 13th, 2008
0

Re: Program works but shorter code?

Sorry, more robust code (Ctrl-Z reaction added):
cpp Syntax (Toggle Plain Text)
  1. string word;
  2.  
  3. while (cout << "Enter a word which includes NO i's : ",
  4. cin >> word &&
  5. word.find_first_of('iI') != string::npos
  6. )
  7. cout << "I asked for no i's!" << endl;
  8. cout << "Thank you. Bye..." << endl;string word;
Last edited by Tekmaven; Jul 13th, 2008 at 5:29 pm. Reason: Added syntax to code tags
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jul 13th, 2008
0

Re: Program works but shorter code?

Click to Expand / Collapse  Quote originally posted by ArkM ...
Sorry, more robust code (Ctrl-Z reaction added):
cpp Syntax (Toggle Plain Text)
  1. string word;
  2.  
  3. while (cout << "Enter a word which includes NO i's : ",
  4. cin >> word &&
  5. word.find_first_of('iI') != string::npos
  6. )
  7. cout << "I asked for no i's!" << endl;
  8. 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?
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 13th, 2008
0

Re: Program works but shorter code?

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...tml/index.html

C++ Syntax (Toggle Plain Text)
  1. #include <boost/regex.hpp>
  2. #include <iostream>
  3. #include<string>
  4.  
  5. int main()
  6. {
  7. std::string word;
  8. boost::regex e = "\b.+?[^'].+\b";
  9. boost::smatch what;
  10. std::cout << "Enter a word";
  11. std::cin >> word;
  12. if(boost::regex_match(word, what, e)
  13. {
  14. std::cout << "The word can not include ' " << std::endl;
  15. }
  16. std::cout << "Thank you. Bye..." << std::endl;
  17. }
Reputation Points: 10
Solved Threads: 2
Newbie Poster
galin is offline Offline
10 posts
since Aug 2007
Jul 13th, 2008
-1

Re: Program works but shorter code?

Click to Expand / Collapse  Quote originally posted by galin ...
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...tml/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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Jul 14th, 2008
0

Re: Program works but shorter code?

Some explanations:

Stream operator >> (and << too) returns reference to stream (that's why we may couple expressions in
cpp Syntax (Toggle Plain Text)
  1. cin >> first >> second;
  2. // (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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Hi I need of procedural
Next Thread in C++ Forum Timeline: ethernet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC