| | |
Program works but shorter code?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 14
Reputation:
Solved Threads: 0
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)
// 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:
The loop lines 14-25 can be rewritten like this:
C++ Syntax (Toggle Plain Text)
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 told Santa what I wanted for Christmas and he washed my mouth out with soap.
I guess it is C++ , you can use std::string apis like find , your code will reduce drastically.
@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
You can also remove line 9, the deceleration of the variable i, if you employ this method.
Also, the program terminates after
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)
if( strchr(word,'i') ) { cout << endl << "I asked for no i's!"; }
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...".
Never never never use cin >> char array!
C++ Syntax (Toggle Plain Text)
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):
cpp Syntax (Toggle Plain Text)
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;
Last edited by Tekmaven; Jul 13th, 2008 at 5:29 pm. Reason: Added syntax to code tags
•
•
•
•
Sorry, more robust code (Ctrl-Z reaction added):
cpp Syntax (Toggle Plain Text)
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;
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Aug 2007
Posts: 10
Reputation:
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...tml/index.html
C++ Syntax (Toggle Plain Text)
#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...tml/index.html
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Some explanations:
Stream operator >> (and << too) returns reference to stream (that's why we may couple expressions in
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...
Stream operator >> (and << too) returns reference to stream (that's why we may couple expressions in
cpp Syntax (Toggle Plain Text)
cin >> first >> second; // (cin >> first) >> second - again stream >> target
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...
![]() |
Similar Threads
- sorting the "news" (C)
- Open In New Window Php (PHP)
- Candidate Program (C++)
- currency conversion program error (C++)
- continuous string (C)
- help with parrallel arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: Hi I need of procedural
- Next Thread: ethernet
Views: 1348 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






