| | |
function not working cant figure problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Solved Threads: 0
been working on this code for 2-3 hours using the basic c++ information. tearing out my hair trying to figure out what is wrong writing hangman programm but trying to use functions to initialize programm and enter and response computer saying little or no error but wont launch can't figure out what to do plz help me. there are two types ive tried
OPTION 1:
OPTION 2:
OPTION 1:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> using namespace std; char getGuess(); string inWord(); int main() { // set-up const int MAX_WRONG = 8; vector<string> words; words.push_back("GUESS"); words.push_back("HANGMAN"); words.push_back("DIFFICULT"); srand(time(0)); random_shuffle(words.begin(), words.end()); const string THE_WORD = words[0]; int wrong = 0; string soFar(THE_WORD.size(), '-'); string used = ""; cout << "welcome to hangman!!"; }char getGuess() { char guess; cout << "\n\nEnter your guess: "; cin >> guess; guess = toupper(guess); return guess; } string inWord() { vector<string> words; char guess; const string THE_WORD = words[0]; char guess1 = THE_WORD.find(guess); int wrong = 0; string soFar(THE_WORD.size(), '-'); if (guess1 != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; for (int i = 0; i < THE_WORD.length(); ++i) if (THE_WORD[i] == guess) soFar[i] = guess; } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrong; } return soFar; } while ((wrong < MAX_WRONG) && (soFar != THE_WORD)); { cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n"; cout << "\nYou've used the following letters:\n" << used << endl; cout << "\nSo far, the word is:\n" << soFar << endl; getGuess() while (used.find(guess) != string::npos) { cout << "\nYou've already guessed " << guess << endl; cout << "Enter your guess: "; cin >> guess; guess = toupper(guess); } used += guess; inWord() // shut down if (wrong == MAX_WRONG) cout << "\nYou've been hanged!"; else cout << "\nYou guessed it!"; cout << "\nThe word was " << THE_WORD << endl; return 0; }
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> using namespace std; char getGuess(); string inWord(); int main() { const int MAX_WRONG = 8; char guess; vector<string> words; words.push_back("GUESS"); words.push_back("HANGMAN"); words.push_back("DIFFICULT"); srand(time(0)); random_shuffle(words.begin(), words.end()); const string THE_WORD = words[0]; int wrong = 0; string soFar(THE_WORD.size(), '-'); string used = ""; cout << "welcome to hangman!!"; while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) { cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n"; cout << "\nYou've used the following letters:\n" << used << endl; cout << "\nSo far, the word is:\n" << soFar << endl; getGuess(); while (used.find(guess) != string::npos) { cout << "\nYou've already guessed " << guess << endl; cout << "Enter your guess: "; cin >> guess; guess = toupper(guess); } used += guess; inWord(); if (wrong == MAX_WRONG) cout << "\nYou've been hanged!"; else cout << "\nYou guessed it!"; cout << "\nThe word was " << THE_WORD << endl; return 0; } } char getGuess() { char guess; cout << "\n\nEnter your guess: "; cin >> guess; guess = toupper(guess); return guess; } string inWord() { vector<string> words; char guess; const string THE_WORD = words[0]; char guess1 = THE_WORD.find(guess); int wrong = 0; string soFar(THE_WORD.size(), '-'); if (guess1 != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; for (int i = 0; i < THE_WORD.length(); ++i) if (THE_WORD[i] == guess) soFar[i] = guess; } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrong; } return soFar; }
Last edited by Ancient Dragon; Jul 8th, 2008 at 8:42 am. Reason: add code tags
•
•
Join Date: Jun 2008
Posts: 7
Reputation:
Solved Threads: 0
We dont use code-tags in our c++ tags but if you guys say they are suppose to be there i might have to self teach myself them because we are using he C++ beginning from thomson course technology. and it doesnt mention code-tags
>and it doesnt mention code-tags
Code tags are a feature of Daniweb's forum. You take your code and wrap it in code tags like so when you want to post it here:
[code=cplusplus]
<code from your book here>
[/code]
This preserves the formatting of your code rather than trimming all leading whitespace from it. With the cplusplus attribute, line numbers and color coding are also added. Here's how code looks without code tags:
#include <iostream>
int main()
{
for ( int i = 0; i < 10; i++ )
std::cout<<"Hello, world!\n";
}
And here's the same code with code tags:
Notice the difference?
p.s. We give you about 10 posts to learn how to use code tags, then start issuing infractions for each failure to use them. At 2 points per infraction, that means you can get off five posts before you're automatically banned for not following our rules.
Code tags are a feature of Daniweb's forum. You take your code and wrap it in code tags like so when you want to post it here:
[code=cplusplus]
<code from your book here>
[/code]
This preserves the formatting of your code rather than trimming all leading whitespace from it. With the cplusplus attribute, line numbers and color coding are also added. Here's how code looks without code tags:
#include <iostream>
int main()
{
for ( int i = 0; i < 10; i++ )
std::cout<<"Hello, world!\n";
}
And here's the same code with code tags:
C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { for ( int i = 0; i < 10; i++ ) std::cout<<"Hello, world!\n"; }
p.s. We give you about 10 posts to learn how to use code tags, then start issuing infractions for each failure to use them. At 2 points per infraction, that means you can get off five posts before you're automatically banned for not following our rules.
I'm here to prove you wrong.
![]() |
Similar Threads
- Sony Vaio Fn key problem (USB Devices and other Peripherals)
- Shopping Cart Not Working (ASP.NET)
- C++ function to check validity of sudoku (C++)
- for loop not working right (C++)
- Wireless Connection Excellent but working intermitently (Networking Hardware Configuration)
- Problem with Turbo C...or so I think!! (C++)
- round robin scheduling (C)
Other Threads in the C++ Forum
- Previous Thread: Transferring images..
- Next Thread: Re: Making a .dat file
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






