| | |
C++ Programming issue
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
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.
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 ***
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)
// A simple little hangman type game // //included libraries #include <iostream> #include <stdlib> #include <conio> #include <stdio> #include <fstream> #include <string> using namespace std; // begin program void main() { char getWord[256]; // variable for the inital word int numLetters; //variable for the number of letters in the word cout << "Welcome to the hangman program"<<endl; cout <<"\n Please enter a word" << endl; cin >>getWord; strlen(getWord); printf ("The word entered has %u letters.\n",strlen(getWord)); strlen(getWord)=numLetters; while(numLetters > 0) { cout << "_"; numLetters--; } }
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
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
Thanks Infarction. it fixed it. I modified code based on your suggestions as such:
c++ Syntax (Toggle Plain Text)
// A simple little hangman type game // //included libraries #include <iostream> #include <stdlib> #include <conio> #include <stdio> #include <fstream> #include <string> using namespace std; // begin program void main() { string getWord; // variable for the inital word int numLetters; //variable for the number of letters in the word cout << "Welcome to the hangman program"<<endl; cout <<"\n Please enter a word" << endl; cin >>getWord; getWord.length(); printf ("The word entered has %u letters.\n",getWord.length()); numLetters=getWord.length(); while(numLetters > 0) { cout << "_ "; numLetters--; } }
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
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
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
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_.
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?
c++ Syntax (Toggle Plain Text)
// A simple little hangman type game // //included libraries #include <iostream> #include <stdlib> #include <stdio> #include <fstream> #include <string> using namespace std; char dashes(int); // begin program int main() { string getWord; // variable for the inital word int numLetters; //variable for the number of letters in the word int numGuesses; //variable for the number of guesses the contestant has remaining. char guessLetter;//variable for the letter that the contestant guesses. char foundLetter; //variable for letters in the word 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'}; int i; cout << "Welcome to the hangman program"<<endl; cout <<"\n Please enter a word" << endl; cin >>getWord; printf ("The word entered has %u letters.\n",getWord.length()); numLetters=getWord.length(); // get the number of letters in the word the user input while(numLetters > 0) //prints out the _, one for each letter { cout << "_ "; numLetters--; } numGuesses=getWord.length()*2; //gets the number of guesses for the inital and the remaining guesses for the contestant if (numGuesses == getWord.length()*2) { cout << "You have "<<numGuesses <<" guesses." <<endl; } else cout << "You have "<<numGuesses <<"remaining." <<endl; cout << "Guess a letter" <<endl; cin >> guessLetter; while (guessLetter != letterList[i]) { cout << "Your guess was not a letter, please guess again."; } if (foundLetter = getWord.find(guessLetter)) { cout << "Your guess was correct" <<endl; cout << guessLetter << dashes(numLetters) <<endl; numGuesses--; cout << "You have "<<numGuesses <<" remaining."<<endl; } else { numGuesses--; cout << "Your guess was incorrect. You have "<<numGuesses <<" remaining." <<endl; } return 0; } char dashes() { while(numLetters > 0) //prints out the _, one for each letter { cout << "_ "; numLetters--; } }
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
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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.
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.
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
I am trying to convert this
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:
•
•
•
•
string displayString ;
int len = getWord.length();
for(int i = o; i < len; ++i)
displayString += "_";
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)
// A simple little hangman type game // //included libraries #include <iostream> #include <stdlib> #include <stdio> #include <fstream> #include <string> using namespace std; string displayString(string); //prototype header for displayString function // begin program int main() { string getWord; // variable for the inital word int numLetters; //variable for the number of letters in the word int numGuesses; //variable for the number of guesses the contestant has remaining. char guessLetter;//variable for the letter that the contestant guesses. char foundLetter; //variable for letters in the word 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'}; int i; cout << "Welcome to the hangman program"<<endl; cout <<"\n Please enter a word" << endl; cin >>getWord; printf ("The word entered has %u letters.\n",getWord.length()); // get the number of letters in the word the user input /*while(numLetters > 0) //prints out the _, one for each letter { cout << "_ "; numLetters--; } */ cout << displayString; numGuesses=getWord.length()*2; //gets the number of guesses for the inital and the remaining guesses for the contestant if (numGuesses == getWord.length()*2) { cout << "You have "<<numGuesses <<" guesses." <<endl; } else cout << "You have "<<numGuesses <<"remaining." <<endl; cout << "Guess a letter" <<endl; cin >> guessLetter; while (guessLetter != letterList[i]) { cout << "Your guess was not a letter, please guess again."; break; } if (getWord.find(guessLetter)) { cout << "Your guess was correct" <<endl; //cout << guessLetter << <<endl; numGuesses--; cout << "You have "<<numGuesses <<" remaining."<<endl; } else { numGuesses--; cout << "Your guess was incorrect. You have "<<numGuesses <<" remaining." <<endl; } return 0; } displayString(&getWord) { int len = getWord.length(); for(int i = 0; i < len; ++i) displayString += "_"; numLetters=getWord.length(); }
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
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
Here are the 3 parts
part 1
Part 2 (compares the input "strings")
Part 3 (makes it case insensitive)
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)
#include <iostream> #include <string> using namespace std; int main() { char getWord[50]; char goodGuesses[256] = {'\0'}; char badGuesses[256] = {'\0'}; bool badGuess = true; char guess; int guessCount; int guessCountLimit; bool sameWord = true; int t; int r; int s; cout << "Please enter a word" << endl; cin >> getWord; //find length of target int i = 0; while(getWord[i] != '\0') //stop at null terminator i++; guessCountLimit = i*2; cout << "Guess a letter: "; cin >> guess; guessCount = 0; i = 0; int j = 0; int k = 0; while(guessCount <= guessCountLimit) { while(getWord[i] != '\0') { if(getWord[i] == guess) { goodGuesses[j] = guess; badGuess = false; j++; //advance position in goodGuesses } i++; } if(badGuess) { badGuesses[k] = guess; k++; //advance position in badGuesses } i = 0; //reset to go through target again badGuess = true; cout << "Good guesses: " << goodGuesses << endl; cout << "Bad guesses: " << badGuesses << endl; guessCount++; //get next guess cout << "Guess a letter: "; cin >> guess; } //Load up r i= 0; while(t[i]) { r[i] = '-'; i++; } //end r with null terminator r[i] = '\0'; cout << t << " " << r << endl; cout << "Guess: "; cin >> guess; i = 0; while(guessCount !=guessCountLimit) { while(t[i]) { //cout << guess << " = " << t[i] << endl; if(guess == t[i]) r[i] = guess; i++; } i = 0; while(sameWord && t[i]) { cout << int(t[i]) << " = " << int(r[i]) << endl; if(t[i] != r[i]) sameWord = false; i++; } i = 0; if(sameWord) break; sameWord = true; cout << r << endl; cout << "Guess: "; cin >> guess; } }
Here are the 3 parts
part 1
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main() { char getWord[50]; char goodGuesses[256] = {'\0'}; char badGuesses[256] = {'\0'}; bool badGuess = true; char guess; int guessCount; int guessCountLimit; cout << "Please enter a word" << endl; cin >> getWord; //find length of target int i = 0; while(getWord[i] != '\0') //stop at null terminator i++; guessCountLimit = i; cout << "Guess a letter: "; cin >> guess; guessCount = 0; i = 0; int j = 0; int k = 0; while(guessCount <= guessCountLimit) { while(getWord[i] != '\0') { if(getWord[i] == guess) { goodGuesses[j] = guess; badGuess = false; j++; //advance position in goodGuesses } i++; } if(badGuess) { badGuesses[k] = guess; k++; //advance position in badGuesses } i = 0; //reset to go through target again badGuess = true; cout << "Good guesses: " << goodGuesses << endl; cout << "Bad guesses: " << badGuesses << endl; guessCount++; //get next guess cout << "Guess a letter: "; cin >> guess; } }
Part 2 (compares the input "strings")
c++ Syntax (Toggle Plain Text)
//end r with null terminator r[i] = '\0'; cout << t << " " << r << endl; cout << "Guess: "; cin >> guess; i = 0; while(1) { while(t[i]) { //cout << guess << " = " << t[i] << endl; if(guess == t[i]) r[i] = guess; i++; } i = 0; while(sameWord && t[i]) { cout << int(t[i]) << " = " << int(r[i]) << endl; if(t[i] != r[i]) sameWord = false; i++; } i = 0; if(sameWord) break; sameWord = true; cout << r << endl; cout << "Guess: "; cin >> guess; }
c++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char a[] = "Hello"; char b[256]; //load up b with capped a int i = 0; for( ; a[i]; i++) { b[i] = a[i]; //cap non-capped if(b[i] > 96 && b[i] < 123) b[i] = b[i] - 32; } b[i] = '\0'; cout << a << " " << b << endl; return 0; }
![]() |
Similar Threads
- General Programming issue (Computer Science)
- problem in hotmail (Java)
Other Threads in the C++ Forum
- Previous Thread: isinvalidInt() ??????
- Next Thread: C++ file to array
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






