• c++ word inventor
    44 replies, posted
[cpp] #include <iostream> #include <ctime> #include <cstdlib> int main() { srand((unsigned)time(NULL)); for(int i = 0; i < 100; i++) { std::cout << (rand()%100) + 1 << std::endl; } return 0; } [/cpp] There. I just used C because srand, rand and time are C functions so it felt more natural. [QUOTE=gparent;17920043]This will probably work in your case, although you use a recursive main (which is a big no-no).[/QUOTE] Agreed; there are a lot of things you could do better with your code, but using a loop instead is a big first step.
Made things faster, portable and a little cleaner - therefore removed some features (such as console title & color). I tried to keep your coding-style and variable-names. [cpp]#include <cstddef> #include <cstdlib> #include <ctime> #include <algorithm> #include <limits> #include <ios> #include <iostream> #include <string> using namespace std; static const string vocals = "aeiou"; //randomization function called if a vowel must be randomly choosed inline char vocalrand(){ return vocals[static_cast<size_t>(rand()%4)]; } inline bool is_vocal(char c){ return vocals.find(c)!=string::npos; } //randomization function that helps randomly choose a character from the 26 available inline char randomi(){ return 'a'+static_cast<char>(rand()%25); } int main(){ size_t n=1, conson; string cuv; ios::sync_with_stdio(false); //makes streams faster srand(static_cast<unsigned int>(time(NULL))); cout<<" Type the number of characters the word will have\n\n" <<" The optimal number of characters is between 7 and 10 , under 5 the word won't always be readable and more than 15 will make it unreadable\n\n"<<flush; while(n==1) { //don't care about fancy title and colors for better platform independence conson=0; cout<<"Number of characters:"<<endl; while(!(cin>>n)){ cout<<"Invalid input!\n"<<flush; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } cout<<"\n\n"; cuv.resize(n); for(string::iterator contor=cuv.begin(); contor!=cuv.end(); ++contor){ *contor=randomi(); if(is_vocal(*contor)){ conson=0; } else{ ++conson; } if(conson==2){ *contor=vocalrand(); conson=0; } } cout<<" The generated word is:\n" <<cuv<<"\n\n" <<"Try again ?(yes=1, no=0)"<<endl; while((!(cin>>n))&&(n!=1)&&(n!=0)){ cout<<"Invalid input!\n"<<flush; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } cout<<"\n\n"<<flush; } cout<<"bye!\n" <<"(press enter to quit)"<<endl; cin.ignore(numeric_limits<streamsize>::max(), '\n').get(); }[/cpp] If you - or anyone else - has questions about the code, I don't mind answering. Same goes for further fixes/optimizations :)
You could just do this: [cpp] #ifdef _WIN32 #include <windows.h> #endif int main() { #ifdef _WIN32 SetConsoleTitle("Title Here"); #endif ... } [/cpp]
Wow ummm. Thank you very much. I'll read the code and come with further questions. [EDIT] Well, I understand nothing :(. You made it in a much more advanced way than I know yet. I think it would help if you commented every line :D. You made everything confusing to me :( [EDIT] Ok, I do unsterstand a part of the code at a second read, but there are elements that I simply haven't heard of. I'll move to the next chapter in my c++ book ( written by Brian Overland) :D.
Ah well, I guess you are not too familiar with the STL. I'd say, look up the classes and functions you don't know in your book or the internet. I commented the whole code and made a small optimization to the loop: [cpp]#include <cstddef> //NULL (for time), size_t #include <cstdlib> //srand, rand #include <ctime> //time (for srand) #include <limits> //numeric_limits #include <ios> //sync_with_stdio(false) #include <iostream> #include <string> using namespace std; //import the whole std-namespace; do not use "using" in header-files! static const string vocals = "aeiou"; //inline will hint at the compiler that the functions are very small and so the function-calls could be replaced by the code inside the functions -> more performance, little to no memory-gain (as in won't require more memory) //randomization function called if a vowel must be randomly choosed inline char vocalrand(){ return vocals[static_cast<size_t>(rand()%4)]; //rand()%4 returns a value in [0..4], cast to size_t to access vocals and return one of the 5 chars } inline bool is_vocal(char c){ return vocals.find(c)!=string::npos; //find returns the position of the char, or if it was not found then std::string::npos } //randomization function that helps randomly choose a character from the 26 available inline char randomi(){ return 'a'+static_cast<char>(rand()%25); //rand()%25 returns a value in [0..25]; add that to 'a' to make the function return a value in ['a'..'z'] } int main(){ size_t n=1, conson; string cuv; ios::sync_with_stdio(false); //makes streams faster by not synchronizing with c-streams (printf, ...), which it does by default srand(static_cast<unsigned int>(time(NULL))); //seed by the time the computer has been running (cast to unsigned int because time returns long) cout<<" Type the number of characters the word will have\n\n" <<" The optimal number of characters is between 7 and 10 , under 5 the word won't always be readable and more than 15 will make it unreadable\n\n"<<flush; while(n==1){ //1=yes, 0=no //don't care about fancy title and colors for better platform independence conson=0; cout<<"Number of characters:"<<endl; while(!(cin>>n)){ //if std::cins operator>> failed, it will be invalid (fail-bit set, accesible though operator!) cout<<"Invalid input!\n"<<flush; cin.clear(); //clear fail-bit cin.ignore(numeric_limits<streamsize>::max(), '\n'); //ignore rest of the input } cout<<"\n\n"; cuv.resize(n); //precache n characters in string for(string::iterator contor=cuv.begin(); contor!=cuv.end(); ++contor){ //loop through the whole word if(conson==2){ *contor=vocalrand(); //2 consonants -> must be vowel! conson=0; //reset conson } else{ *contor=randomi(); //make the char a random character if it is not required to be a vovel if(is_vocal(*contor)){ conson=0; //reset conson if the character was a vovel } else{ ++conson; //if it was not a vovel, up conson by 1 } } } cout<<" The generated word is:\n" <<cuv<<"\n\n" <<"Try again ?(yes=1, no=0)"<<endl; while((!(cin>>n))&&(n!=1)&&(n!=0)){ //same as before, but added check that n must be 1 or 0 cout<<"Invalid input!\n"<<flush; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } cout<<"\n\n"<<flush; } cout<<"bye!\n" <<"(press enter to quit)"<<endl; cin.ignore(numeric_limits<streamsize>::max(), '\n').get(); //ignore all former input and then wait for another (press enter to quit) for those, who don't run in command-line }[/cpp]
I must say, it may not be best to stuff all this information in him while he's still learning the basics.
I optimized the program and made it generate words instantly. Also, you can now generate a lot of words at once, randomly. I also made a loop instead of a recursive main() function :D. [url]http://sjackm.wordpress.com/2009/10/19/the-program-that-makes-words-c/[/url] and acces the [b]Download-Beta3[/b] link :P I'm sorry I didn't follow your code to redo the program, but I burrowed some "coding style" from it :P.
Still doesn't work for my Win 7 64-bit. My VS2010 debugger says it's an stackoverflow, but of course I can't tell you the line since I don't have the code ;)
[code]#include <iostream> #include <windows.h> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; //randomization function called if a vowel must be randomly choosed int vocalrand(){ int v; v=rand()%5; //cout<<"V is equal to "<<v<<" "<<endl; //cout<<" "<<endl; return v; } //randomization function that helps randomly choose a character from the 26 available int randomi(){ int n; n=rand()%26; //cout<<" N is equal to "<<n<<" (the number of the character in the array) "<<endl; //cout<<" "<<endl; return n; } int main(){ srand(time(NULL)); system("title Generatorul de Cuvinte"); // program name system("color 20"); // system color int n,contor,rand_liter,rand_vocal,conson,restart,muchword; // n=nr of characters, contor is used to move to the next character, rand_liter is used for the random character generation, rand_vocal generates a random vowel, conson is used to verify if there are more than 2 consonants and the next character is still a consonant ,restart is for checking the reset condition double secunde; char cuv[n]; char *liter="abcdefghijklmnopqrstuvwxyz"; // the 26 characters array ( or string ? don't really know, I'm a beginner. But I know it works, and I spent 1 hour trying to make a 26 character array :D) restart=1; // initial restart value must be 1 in order for the program to run at least once ! while(restart==1){ // the the biggest loop : this loop covers all the program, for restarting or quiting cout<<" Type the number of characters the word will have"<<endl; cout<<" "<<endl; cout<<" The text that's generated while the characters are generated is not important"<<endl; cout<<" "<<endl; cout<<" The optimal number of characters is between 7 and 10 , under 5 the word won't always be readable and more than 15 will make it unreadable "<<endl; cout<<" "<<endl; cout<<"Number of characters:"<<endl; cin>>n; cout<<" "<<endl; cout<<" How many words do you wish to generate for that amount of characters ?"<<endl; cout<<" Words: "<<endl; cin>>muchword; for(int i=0;i<=muchword;i++){ //the main loop : the whole code is executed again, according to the number of words given. cout<<" "<<endl; contor=0; while(contor<n){ // the code that does the job starts here. ok, please don't scream OMG when you see the if condition, I know I could have used negative checks and it would be a lot smaller, but I realised that too late and was too lazy to delete the line and redo it :P rand_liter=randomi(); if((rand_liter==1)||(rand_liter==2)||(rand_liter==3)||(rand_liter==5)||(rand_liter==6)||(rand_liter==7)||(rand_liter==9)||(rand_liter==10)||(rand_liter==11)||(rand_liter==12)||(rand_liter==13)||(rand_liter==15)||(rand_liter==16)||(rand_liter==17)||(rand_liter==18)||(rand_liter==19)||(rand_liter==21)||(rand_liter==22)||(rand_liter==23)||(rand_liter==24)||(rand_liter==25)){ ++conson; //cout<<" conson++ "; } if(conson>=2){ //cout<<" 2 consons true "<<endl; rand_vocal=vocalrand(); conson=0; switch(rand_vocal){ case 0: cuv[contor]='u'; break; case 1: cuv[contor]='a'; break; case 2: cuv[contor]='e'; break; case 3: cuv[contor]='i'; break; case 4: cuv[contor]='o'; break; }} else{ cuv[contor]=liter[rand_liter]; } ++contor; //cout<<"====================================================="<<endl; //cout<<" "<<endl; } // the main code "while" loop is ending here if(i==0){ cout<<" "<<endl; cout<<" "<<endl; cout<<" The generated word(s) is(are): "<<endl; } cout<<cuv<<endl; cout<<" "<<endl; if(i==0){ cout<<" The weird characters from the end of the word are not important"<<endl; cout<<" "<<endl; } cout<<"|-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-||-|-|"<<endl; cout<<" "<<endl; } // the main "for" loop ends here cout<<" CopyRight SamuraiJack -> marcel_max2006@yahoo.com, 2009 "<<endl; cout<<" "<<endl; cout<<"Try again ?(yes=1, no=0) "<<endl; cout<<" "<<endl; cout<<" "<<endl; cout<<" =>Version History<= "<<endl; cout<<" Beta1: Basic Program finished."<<endl; cout<<" Beta2: Made words 'readable'( in the way that they won't be something like fnrefjkds, but more like aitoruperlo(this is just an example :P))"<<endl; cout<<" Changed window color to green"<<endl; cout<<" Beta3: Word generation is now made instantly."<<endl; cout<<" Made possible to choose how many words you want to generate at once for the specified number of characters"<<endl; cout<<" Program now uses a loop instead of a recursive main() function :D."<<endl; cin>>restart; while(restart!=1&&restart!=0){ cout<<"Invalid value entered. Try again ( Yes=1, No=0)"<<endl; cin>>restart; } system("CLS"); } // the ultra big while loop that covers all the code ends here return 0; } [/code] There, now you have it :)
char cuv[n]; That's the line. It doesn't even compile for me, but you'd have to initialize n to something, else it's a (more or less) random variable and thus could be something way too big for the stack. What compiler are you using? Also, did you try making a false input? The program will then loop; you need to clear the fail-flag (cin.clear()) and flush the input (cin.ignore(numeric_limits<streamsize>::max(), '\n')). It's also funny what happens when you want a word of -1 characters or -1 words *hint,hint*
[quote]int n[/quote] I initialized it :\ I use dev-c++ [quote]It's also funny what happens when you want a word of -1 characters or -1 words *hint,hint* [/quote] Well, I won't make something with a commercial aspect anyway :P. I'm just testing my ideas for fun, trying to make then faster and add new features.
[cpp] int n,contor,rand_liter,rand_vocal,conson,restart,muchword; // n=nr of characters, contor is used to move to the next character, rand_liter is used for the random character generation, rand_vocal generates a random vowel, conson is used to verify if there are more than 2 consonants and the next character is still a consonant ,restart is for checking the reset condition double secunde; char cuv[n];[/cpp] No, you did not initialize it. Dev-C++ is out-dated. Get Code::Block or CodeLite; since you are on Windows you might also wanna look into Microsoft Visual Studio Express (yes, it's free). Also, you can simply check if the input was < 0, and if so, ask again.
[QUOTE=SamuraiJack;17933763]I initialized it :\ I use dev-c++ Well, I won't make something with a commercial aspect anyway :P. I'm just testing my ideas for fun, trying to make then faster and add new features.[/QUOTE] declaration != initialization [code] int n; //Declaration int n = 0; //initialization [/code]
[QUOTE=danharibo;17934044]declaration != initialization [code] int n; //Declaration int n = 0; //initialization [/code][/QUOTE] Actually in that case int n; is a definition, not a declaration. extern int n; would be a declaration.
Sorry, you need to Log In to post a reply to this thread.