944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3796
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 14th, 2007
0

C++ Programming issue

Expand Post »
I am writing a simple (extremely simple) hangman program. Basically the user inputs a word, then they have double the number of letters in the word for guesses (for instance if the word has 7 letters they get 14 guesses). Please don't ask why I want the user to input the word, because it is a homework assignment and that is what the instructor is wanting us to do.

I am having a problem getting the strlen() to write the value to another variable so that I can double it to get the # of guesses and also get one "_" per letter.
c++ Syntax (Toggle Plain Text)
  1. // A simple little hangman type game
  2. //
  3. //included libraries
  4. #include <iostream>
  5. #include <stdlib>
  6. #include <conio>
  7. #include <stdio>
  8. #include <fstream>
  9. #include <string>
  10.  
  11. using namespace std;
  12. // begin program
  13. void main()
  14. {
  15. char getWord[256]; // variable for the inital word
  16. int numLetters; //variable for the number of letters in the word
  17.  
  18. cout << "Welcome to the hangman program"<<endl;
  19. cout <<"\n Please enter a word" << endl;
  20. cin >>getWord;
  21. strlen(getWord);
  22. printf ("The word entered has %u letters.\n",strlen(getWord));
  23. strlen(getWord)=numLetters;
  24. while(numLetters > 0)
  25. {
  26. cout << "_";
  27. numLetters--;
  28. }
  29. }

edit: I forgot to mention I am using Borland compiler 5.5 and I am getting the following error:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
hangman1.cpp:
Error E2277 hangman1.cpp 23: Lvalue required in function main()
*** 1 errors in Compile ***
Last edited by Shad0wHawk; Apr 14th, 2007 at 12:37 am. Reason: Forgot some information
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shad0wHawk is offline Offline
8 posts
since Apr 2007
Apr 14th, 2007
0

Re: C++ Programming issue

If you're using C++, you should probably be using the std::string type rather than C-style strings (char*).

And your line strlen(getWord) = numLetters; is backwards. Try flipping it around.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 14th, 2007
0

Re: C++ Programming issue

Thanks Infarction. it fixed it. I modified code based on your suggestions as such:
c++ Syntax (Toggle Plain Text)
  1. // A simple little hangman type game
  2. //
  3. //included libraries
  4. #include <iostream>
  5. #include <stdlib>
  6. #include <conio>
  7. #include <stdio>
  8. #include <fstream>
  9. #include <string>
  10.  
  11. using namespace std;
  12. // begin program
  13. void main()
  14. {
  15. string getWord; // variable for the inital word
  16. int numLetters; //variable for the number of letters in the word
  17.  
  18. cout << "Welcome to the hangman program"<<endl;
  19. cout <<"\n Please enter a word" << endl;
  20. cin >>getWord;
  21. getWord.length();
  22. printf ("The word entered has %u letters.\n",getWord.length());
  23. numLetters=getWord.length();
  24. while(numLetters > 0)
  25. {
  26. cout << "_ ";
  27. numLetters--;
  28. }
  29. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shad0wHawk is offline Offline
8 posts
since Apr 2007
Apr 14th, 2007
0

Re: C++ Programming issue

>getWord.length();
This line doesn't do anything.

And a couple of other things: change "void main" to "int main" and get rid of <conio>.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 14th, 2007
0

Re: C++ Programming issue

Hello frens

I m a bit new to programming in C++, but still from what i understood this is a pretty good program. Keep posting such programs they will help newbies like me to understand the programming tricks.

As my small contribution i suggets a site www.ITpalace.com , this is a quite informative site and will provide you some interactions regarding programming.

Bye
Soha
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cute_soha is offline Offline
4 posts
since Apr 2007
Apr 14th, 2007
0

Re: C++ Programming issue

Ok, now I am having issues with more sections of code. The first I am trying to create a function that will take getWord.length() and will output the "_" one for each letter remaining. So if the word is say "hippo" and you guess "p" it would look like _ _ pp_.

c++ Syntax (Toggle Plain Text)
  1. // A simple little hangman type game
  2. //
  3. //included libraries
  4. #include <iostream>
  5. #include <stdlib>
  6. #include <stdio>
  7. #include <fstream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. char dashes(int);
  13. // begin program
  14. int main()
  15. {
  16. string getWord; // variable for the inital word
  17. int numLetters; //variable for the number of letters in the word
  18. int numGuesses; //variable for the number of guesses the contestant has remaining.
  19. char guessLetter;//variable for the letter that the contestant guesses.
  20. char foundLetter; //variable for letters in the word
  21. char letterList[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  22. int i;
  23. cout << "Welcome to the hangman program"<<endl;
  24. cout <<"\n Please enter a word" << endl;
  25. cin >>getWord;
  26. printf ("The word entered has %u letters.\n",getWord.length());
  27.  
  28. numLetters=getWord.length(); // get the number of letters in the word the user input
  29. while(numLetters > 0) //prints out the _, one for each letter
  30. {
  31. cout << "_ ";
  32. numLetters--;
  33. }
  34. numGuesses=getWord.length()*2; //gets the number of guesses for the inital and the remaining guesses for the contestant
  35. if (numGuesses == getWord.length()*2)
  36. {
  37. cout << "You have "<<numGuesses <<" guesses." <<endl;
  38. }
  39. else
  40. cout << "You have "<<numGuesses <<"remaining." <<endl;
  41.  
  42. cout << "Guess a letter" <<endl;
  43. cin >> guessLetter;
  44. while (guessLetter != letterList[i])
  45. {
  46. cout << "Your guess was not a letter, please guess again.";
  47. }
  48. if (foundLetter = getWord.find(guessLetter))
  49. {
  50. cout << "Your guess was correct" <<endl;
  51. cout << guessLetter << dashes(numLetters) <<endl;
  52.  
  53. numGuesses--;
  54. cout << "You have "<<numGuesses <<" remaining."<<endl;
  55. }
  56. else
  57. {
  58. numGuesses--;
  59. cout << "Your guess was incorrect. You have "<<numGuesses <<" remaining." <<endl;
  60. }
  61. return 0;
  62. }
  63.  
  64. char dashes()
  65. {
  66. while(numLetters > 0) //prints out the _, one for each letter
  67. {
  68. cout << "_ ";
  69. numLetters--;
  70. }
  71. }


on compile I get the following errors:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
hangman1a.cpp:
Warning W8012 hangman1a.cpp 36: Comparing signed and unsigned values in function
main()
Warning W8060 hangman1a.cpp 49: Possibly incorrect assignment in function main()

Error E2451 hangman1a.cpp 67: Undefined symbol 'numLetters' in function dashes()

Warning W8070 hangman1a.cpp 72: Function should return a value in function dashes()
*** 1 errors in Compile ***

Aside from the undefined symbol issue, my problem is that I need to call numLetters for the dashes, but at the same time I need to decrement the number of dashes by the number of letters that are displayed. I also need a method by which to display the correctly guessed letters in the correct location. So that "hippo" doesn't show up as pp_ _ _ when you guess "p". To resolve this issue would it be feasible (or possible) to change the string into a char array, or is there a better method to do this?
Last edited by Shad0wHawk; Apr 14th, 2007 at 11:36 am. Reason: spelling error
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shad0wHawk is offline Offline
8 posts
since Apr 2007
Apr 14th, 2007
0

Re: C++ Programming issue

The string class overloads the [] operator so individual characters can be accessed just like with a char array. the c_str() method will return a const char* to the char array embedded within the string object if you want to change the string into a char [].

You could do something like this:

string displayString ;
int len = getWord.length();
for(int i = o; i < len; ++i)
displayString += "_";

now displayString has as many underscores as there are letters in getWord. And you can display what evers in displayString by something like:

cout << displayString;

then when user inputs a valid guessLetter you loop through getWord and if guessLetter equals getWord[i] then you assign guessLetter to displayString[i]. After loop is completed you can then just output displayString with all the correct guesses in their appropriate slots with remaining underscores intact.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 15th, 2007
0

Re: C++ Programming issue

I am trying to convert this
Click to Expand / Collapse  Quote originally posted by Lerner ...
string displayString ;
int len = getWord.length();
for(int i = o; i < len; ++i)
displayString += "_";
into a function, but I cannot seem to get anything but more and more errors.
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
hangman1a.cpp:
Warning W8012 hangman1a.cpp 36: Comparing signed and unsigned values in function
main()
Error E2303 hangman1a.cpp 64: Type name expected
Error E2356 hangman1a.cpp 64: Type mismatch in redeclaration of 'displayString(s
tring)'
Error E2344 hangman1a.cpp 11: Earlier declaration of 'displayString(string)'
Error E2063 hangman1a.cpp 64: Illegal initialization
Error E2293 hangman1a.cpp 64: ) expected
Error E2141 hangman1a.cpp 70: Declaration syntax error
*** 6 errors in Compile ***

Here is the code that generated the errors:
c++ Syntax (Toggle Plain Text)
  1. // A simple little hangman type game
  2. //
  3. //included libraries
  4. #include <iostream>
  5. #include <stdlib>
  6. #include <stdio>
  7. #include <fstream>
  8. #include <string>
  9.  
  10. using namespace std;
  11. string displayString(string); //prototype header for displayString function
  12. // begin program
  13. int main()
  14. {
  15. string getWord; // variable for the inital word
  16. int numLetters; //variable for the number of letters in the word
  17. int numGuesses; //variable for the number of guesses the contestant has remaining.
  18. char guessLetter;//variable for the letter that the contestant guesses.
  19. char foundLetter; //variable for letters in the word
  20. char letterList[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  21. int i;
  22. cout << "Welcome to the hangman program"<<endl;
  23. cout <<"\n Please enter a word" << endl;
  24. cin >>getWord;
  25. printf ("The word entered has %u letters.\n",getWord.length());
  26.  
  27. // get the number of letters in the word the user input
  28. /*while(numLetters > 0) //prints out the _, one for each letter
  29.   {
  30.   cout << "_ ";
  31.   numLetters--;
  32.   }
  33. */
  34. cout << displayString;
  35. numGuesses=getWord.length()*2; //gets the number of guesses for the inital and the remaining guesses for the contestant
  36. if (numGuesses == getWord.length()*2)
  37. {
  38. cout << "You have "<<numGuesses <<" guesses." <<endl;
  39. }
  40. else
  41. cout << "You have "<<numGuesses <<"remaining." <<endl;
  42.  
  43. cout << "Guess a letter" <<endl;
  44. cin >> guessLetter;
  45. while (guessLetter != letterList[i])
  46. {
  47. cout << "Your guess was not a letter, please guess again.";
  48. break;
  49. }
  50. if (getWord.find(guessLetter))
  51. {
  52. cout << "Your guess was correct" <<endl;
  53. //cout << guessLetter << <<endl;
  54. numGuesses--;
  55. cout << "You have "<<numGuesses <<" remaining."<<endl;
  56. }
  57. else
  58. {
  59. numGuesses--;
  60. cout << "Your guess was incorrect. You have "<<numGuesses <<" remaining." <<endl;
  61. }
  62. return 0;
  63. }
  64. displayString(&getWord)
  65. {
  66. int len = getWord.length();
  67. for(int i = 0; i < len; ++i)
  68. displayString += "_";
  69. numLetters=getWord.length();
  70. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shad0wHawk is offline Offline
8 posts
since Apr 2007
Apr 15th, 2007
0

Re: C++ Programming issue

Why not have an answer string which you load with '_'s and just output that string. As correct guesses are made, replace each '_' with the letter.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Apr 24th, 2007
0

Re: C++ Programming issue

ok, I have completely changed the program. I now have three components that work seperately, but not when I attempt to mesh them.
Here is the code individually. Keep in mind that each seperate function is in fact written as a seperate program and when I mesh them I do change variables a bit so that there is no multiple declarations and such. The error I get is Invalid indirection in function main().

Can someone please help direct me in the proper way to mesh these three pieces together...I was thinking creating 2 parts as header files or some such method.

I am also thinking I am not needing all three parts. Basically I am trying to create the hangman program that uses a users input (the instructor) who will then "guess" the input word, with a mix of capital and non-capital letters and have the program return the word as it is entered (that is if it is Antique it would come out as Antique, AnTiQuE would come out as AnTiQuE and so on) but as long as the letters are there in the correct order it would come out as correct. Also the number of guesses is decremented by 1 each guess. If guesses run out...you are hung. I cannot seem to mesh the parts to come out with a whole though.

This is the attempt at meshing the code:
which causes the aforementioned error

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. char getWord[50];
  10. char goodGuesses[256] = {'\0'};
  11. char badGuesses[256] = {'\0'};
  12. bool badGuess = true;
  13. char guess;
  14. int guessCount;
  15. int guessCountLimit;
  16. bool sameWord = true;
  17. int t;
  18. int r;
  19. int s;
  20. cout << "Please enter a word" << endl;
  21. cin >> getWord;
  22. //find length of target
  23. int i = 0;
  24. while(getWord[i] != '\0') //stop at null terminator
  25. i++;
  26. guessCountLimit = i*2;
  27.  
  28. cout << "Guess a letter: ";
  29. cin >> guess;
  30.  
  31. guessCount = 0;
  32. i = 0;
  33. int j = 0;
  34. int k = 0;
  35. while(guessCount <= guessCountLimit)
  36. {
  37. while(getWord[i] != '\0')
  38. {
  39. if(getWord[i] == guess)
  40. {
  41. goodGuesses[j] = guess;
  42. badGuess = false;
  43. j++; //advance position in goodGuesses
  44. }
  45. i++;
  46. }
  47. if(badGuess)
  48. {
  49. badGuesses[k] = guess;
  50. k++; //advance position in badGuesses
  51. }
  52.  
  53. i = 0; //reset to go through target again
  54. badGuess = true;
  55. cout << "Good guesses: " << goodGuesses << endl;
  56. cout << "Bad guesses: " << badGuesses << endl;
  57.  
  58. guessCount++;
  59.  
  60. //get next guess
  61. cout << "Guess a letter: ";
  62. cin >> guess;
  63. }
  64. //Load up r
  65. i= 0;
  66. while(t[i])
  67. {
  68. r[i] = '-';
  69. i++;
  70. }
  71. //end r with null terminator
  72. r[i] = '\0';
  73.  
  74. cout << t << " " << r << endl;
  75.  
  76.  
  77. cout << "Guess: ";
  78. cin >> guess;
  79.  
  80. i = 0;
  81. while(guessCount !=guessCountLimit)
  82. {
  83. while(t[i])
  84. {
  85. //cout << guess << " = " << t[i] << endl;
  86.  
  87. if(guess == t[i])
  88. r[i] = guess;
  89.  
  90. i++;
  91. }
  92. i = 0;
  93.  
  94. while(sameWord && t[i])
  95. {
  96. cout << int(t[i]) << " = " << int(r[i]) << endl;
  97.  
  98. if(t[i] != r[i])
  99. sameWord = false;
  100.  
  101. i++;
  102. }
  103.  
  104. i = 0;
  105.  
  106. if(sameWord)
  107. break;
  108.  
  109. sameWord = true;
  110.  
  111.  
  112. cout << r << endl;
  113.  
  114. cout << "Guess: ";
  115. cin >> guess;
  116.  
  117. }
  118. }

Here are the 3 parts
part 1
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. char getWord[50];
  10. char goodGuesses[256] = {'\0'};
  11. char badGuesses[256] = {'\0'};
  12. bool badGuess = true;
  13. char guess;
  14. int guessCount;
  15. int guessCountLimit;
  16.  
  17. cout << "Please enter a word" << endl;
  18. cin >> getWord;
  19. //find length of target
  20. int i = 0;
  21. while(getWord[i] != '\0') //stop at null terminator
  22. i++;
  23. guessCountLimit = i;
  24.  
  25. cout << "Guess a letter: ";
  26. cin >> guess;
  27.  
  28. guessCount = 0;
  29. i = 0;
  30. int j = 0;
  31. int k = 0;
  32. while(guessCount <= guessCountLimit)
  33. {
  34. while(getWord[i] != '\0')
  35. {
  36. if(getWord[i] == guess)
  37. {
  38. goodGuesses[j] = guess;
  39. badGuess = false;
  40. j++; //advance position in goodGuesses
  41. }
  42. i++;
  43. }
  44. if(badGuess)
  45. {
  46. badGuesses[k] = guess;
  47. k++; //advance position in badGuesses
  48. }
  49.  
  50. i = 0; //reset to go through target again
  51. badGuess = true;
  52. cout << "Good guesses: " << goodGuesses << endl;
  53. cout << "Bad guesses: " << badGuesses << endl;
  54.  
  55. guessCount++;
  56.  
  57. //get next guess
  58. cout << "Guess a letter: ";
  59. cin >> guess;
  60. }
  61. }

Part 2 (compares the input "strings")
c++ Syntax (Toggle Plain Text)
  1. //end r with null terminator
  2. r[i] = '\0';
  3.  
  4. cout << t << " " << r << endl;
  5.  
  6.  
  7. cout << "Guess: ";
  8. cin >> guess;
  9.  
  10. i = 0;
  11. while(1)
  12. {
  13. while(t[i])
  14. {
  15. //cout << guess << " = " << t[i] << endl;
  16.  
  17. if(guess == t[i])
  18. r[i] = guess;
  19.  
  20. i++;
  21. }
  22. i = 0;
  23.  
  24. while(sameWord && t[i])
  25. {
  26. cout << int(t[i]) << " = " << int(r[i]) << endl;
  27.  
  28. if(t[i] != r[i])
  29. sameWord = false;
  30.  
  31. i++;
  32. }
  33.  
  34. i = 0;
  35.  
  36. if(sameWord)
  37. break;
  38.  
  39. sameWord = true;
  40.  
  41.  
  42. cout << r << endl;
  43.  
  44. cout << "Guess: ";
  45. cin >> guess;
  46.  
  47. }
Part 3 (makes it case insensitive)
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char a[] = "Hello";
  7. char b[256];
  8.  
  9. //load up b with capped a
  10. int i = 0;
  11. for( ; a[i]; i++)
  12. {
  13. b[i] = a[i];
  14. //cap non-capped
  15.  
  16. if(b[i] > 96 && b[i] < 123)
  17. b[i] = b[i] - 32;
  18. }
  19.  
  20. b[i] = '\0';
  21.  
  22. cout << a << " " << b << endl;
  23.  
  24.  
  25. return 0;
  26. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shad0wHawk is offline Offline
8 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: isinvalidInt() ??????
Next Thread in C++ Forum Timeline: C++ file to array





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


Follow us on Twitter


© 2011 DaniWeb® LLC