Program works but shorter code?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 14
Reputation: Code Shark is an unknown quantity at this point 
Solved Threads: 0
Code Shark Code Shark is offline Offline
Newbie Poster

Program works but shorter code?

 
0
  #1
Jul 13th, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Program works but shorter code?

 
0
  #2
Jul 13th, 2008
line 15: does nothing so you might as well delete it.
The loop lines 14-25 can be rewritten like this:
  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. }
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,867
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Program works but shorter code?

 
1
  #3
Jul 13th, 2008
I guess it is C++ , you can use std::string apis like find , your code will reduce drastically.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Program works but shorter code?

 
0
  #4
Jul 13th, 2008
@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
  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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Program works but shorter code?

 
0
  #5
Jul 13th, 2008
Never never never use cin >> char array!
  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;
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Program works but shorter code?

 
0
  #6
Jul 13th, 2008
Sorry, more robust code (Ctrl-Z reaction added):
  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: Program works but shorter code?

 
0
  #7
Jul 13th, 2008
Originally Posted by ArkM View Post
Sorry, more robust code (Ctrl-Z reaction added):
  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?
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: galin is an unknown quantity at this point 
Solved Threads: 2
galin galin is offline Offline
Newbie Poster

Re: Program works but shorter code?

 
0
  #8
Jul 13th, 2008
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Program works but shorter code?

 
-1
  #9
Jul 13th, 2008
Originally Posted by galin View Post
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.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Program works but shorter code?

 
0
  #10
Jul 14th, 2008
Some explanations:

Stream operator >> (and << too) returns reference to stream (that's why we may couple expressions in
  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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1348 | Replies: 14
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC