944,175 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7899
  • C RSS
Feb 27th, 2007
0

string and char functions

Expand Post »
I would like to make this first function work as the one were i opened it from a file. In this first one i am using arrays the second one uses strings. ignore the masking of the password in the first one i want it to do that and then check if the password typed in is correct.
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <ctype.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int ch;
  9. char pword[100];
  10. int i = 0;
  11. char guess[100];
  12. int life = 5;
  13. char key;
  14.  
  15.  
  16. puts ("Enter your password");
  17. fflush(stdout);
  18.  
  19. while ((ch = getch()) != EOF
  20. && ch != '\n'
  21. && ch != '\r'
  22. && i < sizeof(pword) - 1)
  23. {
  24. if (ch == '\b' && i > 0)
  25. {
  26. printf("\b \b");
  27. fflush(stdout);
  28. i--;
  29. pword[i] = '\0';
  30. }
  31. else if (isalnum(ch))
  32. {
  33. putchar('*');
  34. pword[i++] = (char)ch;
  35. }
  36. }
  37.  
  38. pword[i] = '\0';
  39.  
  40. printf ("\nYou entered >%s<", pword);
  41.  
  42. cout << "Player 2. Guess the word" << endl;
  43. cin >> guess;
  44.  
  45. for (int i = 0 ; i < 5 ; i++)
  46. {
  47. pword[i];
  48. }
  49.  
  50. for (int j = 0 ; j < 4 ; j++)
  51. {
  52. cout << guess[j]; //prints the *'s
  53. }
  54.  
  55. while (life !=0) //if lives are greater than 0 carry on else don't
  56. {
  57. cout << "\nplease enter a guess , if you don't guess it right you loose a life" <<endl;
  58.  
  59. cout << "guess is : " ;
  60.  
  61. cin >>key; // grab key from user
  62.  
  63. for (int k = 0; k < 5; k++) //takes guess and checks it over
  64. {
  65. if (key == pword[k])
  66. {
  67. guess[k] = pword[k];
  68. }
  69. }
  70. for (int i = 0; i < pword[5]; ++i)
  71. {
  72. if (key!= pword[i])
  73. {
  74. life --;
  75. cout << "You've just lost a life, you now have "<< life << "remaining\n"<<endl;;
  76. }
  77. }
  78. for (int h = 0; h < 4; h++)
  79. {
  80. cout << guess[h] ;
  81. if( key == guess[h])
  82. {
  83. cout << "WHAT";
  84. return 0;
  85. } }
  86. }
  87. if (life == 0)
  88. {
  89. cout << "Your out of lives, do you wish to play again?" <<endl;
  90. }
  91.  
  92. return 0;
  93. }
  94.  
  95.  
  96. SECOND FUNCTION
  97. //headers
  98.  
  99. string make_display_word(string s) // REPLACES THE STRING WITH *
  100.  
  101. {
  102.  
  103. string t = "";
  104.  
  105. for (int i = 0; i < s.size(); i++)
  106.  
  107. t += '*';
  108.  
  109. return t;
  110.  
  111. }
  112.  
  113. bool ch_in_string(char ch, string s) // CHECKS IF THE CHARACTER ENTERED AND STRING ARE THE SAME
  114.  
  115. {
  116.  
  117. for (int i=0; i < s.size(); i++)
  118.  
  119. if (ch == s[i])
  120.  
  121. return true;
  122.  
  123. return false;
  124.  
  125. }
  126.  
  127. void enter_char(char ch, string secret, string &display) // CHECKS IF THE CHARACTER ENTERED AND STRING ARE THE SAME
  128.  
  129. {
  130.  
  131. for (int i = 0; i < secret.size(); i++) {
  132.  
  133. if (secret[i] == ch)
  134.  
  135. display[i] = ch;
  136.  
  137. }
  138.  
  139. }
  140.  
  141.  
  142.  
  143. // DRAWS THE HANGMAN
  144.  
  145. void play(int figure)
  146.  
  147. {
  148.  
  149. int chance;
  150.  
  151. cout << "How many Chances do you want" << endl;
  152.  
  153. cin >> chance;
  154.  
  155.  
  156.  
  157.  
  158.  
  159. string word ;
  160.  
  161. string q;
  162.  
  163. ifstream fin;
  164.  
  165. fin.open(infile);
  166.  
  167. if(fin.fail())
  168.  
  169. {
  170.  
  171. cerr << "What" << endl;
  172.  
  173. }
  174.  
  175. while(!fin.eof())
  176.  
  177. {
  178.  
  179.  
  180.  
  181. int i;
  182.  
  183.  
  184.  
  185. for(i = 1; i<=10;i++)
  186.  
  187. {
  188.  
  189. i = rand()%100; // Assuming 10,000 words in the file
  190.  
  191.  
  192.  
  193. do
  194.  
  195. {
  196.  
  197. fin>> word;
  198.  
  199. }while ( --i >= 0 );
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. // NUMBER OF WRONG GUESSES
  208.  
  209. int wrong_count = 0;
  210.  
  211. int figure = 1;
  212.  
  213.  
  214.  
  215. string display_word = make_display_word(word);
  216.  
  217. string already_guessed = "";
  218.  
  219.  
  220.  
  221. do
  222.  
  223. {
  224.  
  225. cout << word;
  226.  
  227. cout << "Your guess so far: " << display_word << " "
  228.  
  229. << "Wrong guesses: " << wrong_count
  230.  
  231. << " Letters guessed: " << already_guessed
  232.  
  233. << endl;
  234.  
  235. if (display_word == word && wrong_count ==0)
  236.  
  237. {
  238.  
  239. cout << "Whoa you are great";
  240.  
  241. getch();
  242.  
  243. exit(0);
  244.  
  245. }
  246.  
  247. if (display_word == word)
  248.  
  249. {
  250.  
  251.  
  252.  
  253. cout << "You got it!\n";
  254.  
  255. getch();
  256.  
  257. exit(0);
  258.  
  259. }
  260.  
  261. if (wrong_count >= chance)
  262.  
  263. {
  264.  
  265. cout << "You are dead!\n"
  266.  
  267. << "The correct word was"
  268.  
  269. << " "<<word;
  270.  
  271. getch();
  272.  
  273. exit(0);
  274.  
  275. }
  276.  
  277. cout << "\nGuess a letter: ";
  278.  
  279. char ch;
  280.  
  281. cin >> ch;
  282.  
  283. ch = tolower(ch);
  284.  
  285. if (!isalpha(ch))
  286.  
  287. {
  288.  
  289. cout << "Only letters!\n\n";
  290.  
  291. } else {
  292.  
  293. if (ch_in_string(ch, already_guessed))
  294.  
  295. {
  296.  
  297. cout << "You already guessed '" << ch << "'\n";
  298.  
  299. } else {
  300.  
  301. already_guessed += ch;
  302.  
  303. if (ch_in_string(ch, word))
  304.  
  305. {
  306.  
  307. enter_char(ch, word, display_word);
  308.  
  309. } else { draw(figure);
  310.  
  311. cout << "Sorry, wrong guess!\n";
  312.  
  313. wrong_count++;
  314.  
  315. figure++;
  316.  
  317.  
  318.  
  319.  
  320.  
  321. }
  322.  
  323. }
  324.  
  325. }
  326.  
  327. }while(!false);
  328.  
  329. }
  330.  
  331. }
  332.  
  333. }
Last edited by WaltP; Feb 27th, 2007 at 3:22 am. Reason: Twice I've added CODE tags. It's your turn!
Similar Threads
Reputation Points: 12
Solved Threads: 0
Light Poster
torbecire is offline Offline
46 posts
since Feb 2007
Feb 27th, 2007
0

Re: string and char functions

I don't have a clue what question you are asking.

function ch_in_string() -- there's a lot easier way to do that. std::string has a find method
  1. bool ch_in_string(char ch, string s)
  2. {
  3. return s.find(ch) == string::npos) ? true : false;
  4. }
Last edited by Ancient Dragon; Feb 27th, 2007 at 8:12 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 27th, 2007
0

Re: string and char functions

I don't have a clue what question you are asking.

function ch_in_string() -- there's a lot easier way to do that. std::string has a find method
  1. bool ch_in_string(char ch, string s)
  2. {
  3. return s.find(ch) == string::npos) ? true : false;
  4. }
I guess that should be :
  1. bool char_in_strin (string s, char ch)
  2. {
  3. // return true if char is found in string
  4.  
  5. return ( s.find (ch) != string::npos ? true : false ) ;
  6. }

or in a simple way:
  1. if ( my_string.find (my_char) != string::npos )
  2. {
  3. std::cout << "Character was found in the string \n" ;
  4. }
  5. else
  6. {
  7. std::cout << "Character NOT found in the string \n" ;
  8. }
Last edited by ~s.o.s~; Feb 27th, 2007 at 1:11 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Feb 27th, 2007
0

Re: string and char functions

[Torbecire]

The first function reads a word from a file and replaces characters with '*'. I want the second function to work in the same way, the only difference that we mask the characters that we enter(Like how you would choose a password and not see what you type).
Reputation Points: 12
Solved Threads: 0
Light Poster
torbecire is offline Offline
46 posts
since Feb 2007
Feb 28th, 2007
0

Re: string and char functions

Standard C/C++ does not allow you to type and not echo the characters. Input is buffered which means you type and the system stores the characters until the ENTER is pressed. Then your program reads the characters.

Some -- but not all -- compilers have a way around this, but the functions are
1) not standard so not recommended
2) not necessarily the same for the compilers that do allow this
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is online now Online
7,752 posts
since May 2006

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: Factorial of a number
Next Thread in C Forum Timeline: Problems linking libs, I think...





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


Follow us on Twitter


© 2011 DaniWeb® LLC