C++ Programming issue

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 8
Reputation: Shad0wHawk is an unknown quantity at this point 
Solved Threads: 0
Shad0wHawk Shad0wHawk is offline Offline
Newbie Poster

C++ Programming issue

 
0
  #1
Apr 14th, 2007
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.
  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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: C++ Programming issue

 
0
  #2
Apr 14th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 8
Reputation: Shad0wHawk is an unknown quantity at this point 
Solved Threads: 0
Shad0wHawk Shad0wHawk is offline Offline
Newbie Poster

Re: C++ Programming issue

 
0
  #3
Apr 14th, 2007
Thanks Infarction. it fixed it. I modified code based on your suggestions as such:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: C++ Programming issue

 
0
  #4
Apr 14th, 2007
>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>.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: cute_soha is an unknown quantity at this point 
Solved Threads: 1
cute_soha's Avatar
cute_soha cute_soha is offline Offline
Newbie Poster

Re: C++ Programming issue

 
0
  #5
Apr 14th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 8
Reputation: Shad0wHawk is an unknown quantity at this point 
Solved Threads: 0
Shad0wHawk Shad0wHawk is offline Offline
Newbie Poster

Re: C++ Programming issue

 
0
  #6
Apr 14th, 2007
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_.

  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: C++ Programming issue

 
0
  #7
Apr 14th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 8
Reputation: Shad0wHawk is an unknown quantity at this point 
Solved Threads: 0
Shad0wHawk Shad0wHawk is offline Offline
Newbie Poster

Re: C++ Programming issue

 
0
  #8
Apr 15th, 2007
I am trying to convert this
Originally Posted by Lerner View Post
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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
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: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ Programming issue

 
0
  #9
Apr 15th, 2007
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.
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  
Join Date: Apr 2007
Posts: 8
Reputation: Shad0wHawk is an unknown quantity at this point 
Solved Threads: 0
Shad0wHawk Shad0wHawk is offline Offline
Newbie Poster

Re: C++ Programming issue

 
0
  #10
Apr 24th, 2007
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

  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
  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")
  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)
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC