string and char functions

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

Join Date: Feb 2007
Posts: 46
Reputation: torbecire is an unknown quantity at this point 
Solved Threads: 0
torbecire torbecire is offline Offline
Light Poster

string and char functions

 
0
  #1
Feb 27th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,620
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: 1492
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: string and char functions

 
0
  #2
Feb 27th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: string and char functions

 
0
  #3
Feb 27th, 2007
Originally Posted by Ancient Dragon View Post
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 46
Reputation: torbecire is an unknown quantity at this point 
Solved Threads: 0
torbecire torbecire is offline Offline
Light Poster

Re: string and char functions

 
0
  #4
Feb 27th, 2007
[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).
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,120
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is online now Online
Posting Sensei

Re: string and char functions

 
0
  #5
Feb 28th, 2007
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
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC