Hello, so i am writing a program of hangman but have a few problems that i cant find to solve for i have the right steps but dont know how to resolve the issue. Thsi is what i have so far and needed help. I know i need a do while loop but dont know where to place it and also how to resolve the issue of the string printing it mutiple times. I also need the string to replace with character '*' but i dont know how because if i replace it then all the cahracters in the string will be * instead of its orgianl way.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
// the choice of letter
char choice;
// sentence to find out
string sentence;
//replace the string with * so user does not see them
char replace[] = {};
char star = '*';
//print for the user to see error once
bool once = false;
bool going = true;
// if all parts of body then user has lost
int lost = 0;
cout << "Enter the sentence you will like to enter for the search" << endl;
getline(cin,sentence);
cout << "enter the character you want o find in the string" << endl;
cin >> choice;
cout << "---------------------------" <<endl;
for(int i =0; i < sentence.length(); i++){
// bool will show once because you it once
if (once == false){
if(choice == sentence[i]){
cout << "the choice you enter was in the sentece" << endl;
once = true;
}
else{
if(going == true){
cout << choice << " was not in the sentece " << endl;
lost++;
going == false;
}
cout << "the lost number is " << lost << endl;
switch(lost){
case 1: cout << "head" << endl;
case 2: cout << "body" << endl;
case 3: cout << "left leg" << endl;
case 4: cout << "right leg" << endl;
case 5: cout << "right arm" << endl;
case 6: cout << "left arm" << endl;
}
}
}
}
return 0;
}
@rezy3312,
With respect ot the problem of displaying the string as asterisks, all you have to do is use string.length() function to get the length, then use a for loop to output asterisks for the length of the string.
// Note: This is a complete, tested program.
// You can copy and paste this into your compiler,
// compile and run, and it should not have any issues.
// You should also be able to incorporate it
// into your program without any problems.
#include <string>
#include <iostream>
int main ()
{
std::string sentence;
std::cout << "Enter the sentence for the search. \n: ";
getline (std::cin, sentence);
int len = sentence.length();
for (size_t i = 0; i < len; ++i)
{
// This is so we don't accidentally
// print a * instead of a space.
// The string.at (i) function returns
// the value location "i" in the string.
if (sentence.at (i) != ' ')
{
std::cout << "*";
}
elseif (sentence.at (i) == ' ')
{
std::cout << " ";
}
else
{
continue;
}
}
std::cout << std::endl;
}
Enter the sentence for the search.
: this is a test
**** ** * ****
If you have any questions, feel free to ask!
Good luck!
max
#include <string>
#include <iostream>
int main ()
{
std::string sentence;
std::cout << "Enter the sentence for the search. \n: ";
getline (std::cin, sentence);
for (constchar ch: sentence)
{
// This is so we don't accidentally
// print a * instead of a space.
if (ch == ' ')
{
std::cout << ' ';
}
else
{
std::cout << '*';
}
}
std::cout << std::endl;
}
line 16: int len = sentence.length();
Don't do this. Comparing signed and unsigned values is asking for trouble. Better use auto
@thmm,
Wait, but the int type is unsigned, right? Also, excuse my ignorance, but what is the auto type? I've heard of it but have never used it before, is it from a newer version of C++? (As in, newer than c++03 which is the version I learned).
@seeplus,
Oh yes, I forgot about the ternary operator; I don't use it much, but it is good to know. Comes in real handy for stuff like this.
C++11 changed the language a LOT, C++03 is a dinosaur standard. You might as well try making a mnemonic memory circuit using stone knives and bearskins if you don't understand or won't use C++11 (or later) when you can.
C++14 was mostly bug fixes, C++17 made some radical changes/additions to the language, though not as extensive as C++11 did.
C++20, the current standard though no compiler has fully implemented it yet, is more additions and changes that alter the language. C++2b/C++23 is already being worked on.
You have some serious unlearning/relearning to do to understand Modern C++ (C++11 and later).
@Furry Guy,
Oops, I made a mistake, it wasn't C++03 I learned, it was C++11, which is what I currently use. But I never used the auto type; just never really needed it, I guess.
Hmm, my compiler doesn't have some of that stuff; specifically this:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
// only valid starting in C++20
void addAndPrint (auto x, auto y)
{
std::cout << x + y;
}
int main ()
{
addAndPrint(2, 3);// int
addAndPrint(4.5, 6.7);// double
}
Last login: Mon Mar 15 13:36:56 on ttys001
prandtl:~ agentmax$ cd Desktop/
prandtl:Desktop agentmax$ c++ -std=c++2a -g -o a.out test.cc
test.cc:4:19: error: 'auto' not allowed in function prototype
void addAndPrint (auto x, auto y)
^~~~
test.cc:4:27: error: 'auto' not allowed in function prototype
void addAndPrint (auto x, auto y)
^~~~
2 errors generated.
Even when I use the -std=c++2a flag. Oh well. Needs an update I guess.
So, I would use @seeplus's code, or @thmm's code, and then add in your prompt to guess a letter that's in the message. Then add another for loop, and use that one to iterate through the string and see if the letter is in it. And if it is in the string, then...um...ok, I'm not too familiar with hangman so if someone else could give some input here that would be great.
ok, first thank you guys so much for the help, second, so I made some changes and found it a simpler way for it to print and replace the characters with *. The only problem now is placing the do-while loop, I know i have to use the do-while loop in the section of the for loop but I'm lost on how to do it. This is what i made the changes on but still prints off the whole body. Please help
This mainly makes it easier to read, but it also takes care of possible errors that can result if you omit the curly brackets.
Also, what @seeplus suggested is even simpler than using isalpha() and isspace().
Ok, you also have an issue on line 72:
hangman.cc:72:22: warning: equality comparison result unused [-Wunused-comparison]
incorrect == true;
~~~~~~~~~~^~~~~~~
hangman.cc:72:22: note: use '=' to turn this equality comparison into an assignment
incorrect == true;
^~
=
1 warning generated.
Need to change that to an "=" not a "==".
And, after researching the Hangman game, I think you should clear the screen before printing the asterisks, so you don't have the original sentence up there.
Also, why do you have your printBody() function return something? All it does is prints stuff to cout, you don't actually use the returned value. So get rid of the return statement and change the function to a void (which means it doesn't return anything).
@rezy3312,
Ok, what you need on line 55, where you said "I need a do-while loop," what you need is a search algorithm. I would use a linear search as it is perfect for this sort of thing. Get rid of your for loop in there, (keep the ouput messages and the function call).
Then google "linear search algorithm in c++" and it should give you some good examples.