Hangman
Feb 10, 2013 at 3:54am UTC
I've been working on a Hangman program lately and I've ran into a problem: every time the program runs, the word never randomizes, and there are always 5 underscores. Could someone please help?
Any help is appreciated, thanks.
(sorry for the messy code)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
// Hangman.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <cstdio>
using namespace std;
char guess;
int wordnum;
int thesize;
string theword;
void DisplayUnderscores (int size);
void ScanForLetter (string word,char letter,int size);
void Intro ()
{
cout << " .............................................................................." << endl;
cout << " ....##.....##....###....##....##..######......##.....##....###....##....##...." << endl;
cout << " ....##.....##...##.##...###...##.##....##.....###...###...##.##...###...##...." << endl;
cout << " ....##.....##..##...##..####..##.##...........####.####..##...##..####..##...." << endl;
cout << " ....#########.##.....##.##.##.##.##...####....##.###.##.##.....##.##.##.##...." << endl;
cout << " ....##.....##.#########.##..####.##....##.....##.....##.#########.##..####...." << endl;
cout << " ....##.....##.##.....##.##...###.##....##.....##.....##.##.....##.##...###...." << endl;
cout << " ....##.....##.##.....##.##....##..######......##.....##.##.....##.##....##...." << endl;
cout << " .............................................................................." << endl << endl;
cout << "\t _____ " << endl;
cout << "\t | / | " << endl;
cout << "\t |/ " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t__|________ " << endl;
}
void Intro1 ()
{
cout << " .............................................................................." << endl;
cout << " ....##.....##....###....##....##..######......##.....##....###....##....##...." << endl;
cout << " ....##.....##...##.##...###...##.##....##.....###...###...##.##...###...##...." << endl;
cout << " ....##.....##..##...##..####..##.##...........####.####..##...##..####..##...." << endl;
cout << " ....#########.##.....##.##.##.##.##...####....##.###.##.##.....##.##.##.##...." << endl;
cout << " ....##.....##.#########.##..####.##....##.....##.....##.#########.##..####...." << endl;
cout << " ....##.....##.##.....##.##...###.##....##.....##.....##.##.....##.##...###...." << endl;
cout << " ....##.....##.##.....##.##....##..######......##.....##.##.....##.##....##...." << endl;
cout << " .............................................................................." << endl << endl;
cout << "\t _____ " << endl;
cout << "\t | / | " << endl;
cout << "\t |/ O " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t | " << endl;
cout << "\t__|________ " << endl;
}
int main()
{
srand(time(0));
wordnum = rand() % 10 + 1;
switch (wordnum)
{
case 1:
theword = "rhythm" ;
case 2:
theword = "lynx" ;
case 3:
theword = "photosynthesis" ;
case 4:
theword = "uncopyrightable" ;
case 5:
theword = "hospitalisation" ;
case 6:
theword = "fly" ;
case 7:
theword = "sky" ;
case 8:
theword = "sync" ;
case 9:
theword = "onomatopoeia" ;
case 10:
theword = "satyr" ;
}
thesize = theword.length();
Intro();
DisplayUnderscores(thesize);
cout << endl << endl << "\tGuess a letter: " ;
cin >> guess;
ScanForLetter(theword , guess , thesize);
system ("PAUSE" );
return 0;
}
void DisplayUnderscores (int size)
{
char *Array = new char [size];
Array[size];
for (int p = 0; p<size; p++)
{
Array[p] = '_' ;
}
cout << endl << "\t" << " " ;
for (int p = 0; p<size; p++)
{
cout << Array[p] << " " ;
}
}
void ScanForLetter (string word,char letter,int size)
{
//int size = word.length();
for (int i = 0 ; i < size ; i++)
{
//IF LETTER IS CORRECT #####################################################################################
if (word[i] == letter)
{
}
//if letter is INCORRECT #####################################################################################
if (word[i] != letter)
{
}
}
}
Output everytime:
..............................................................................
....##.....##....###....##....##..######......##.....##....###....##....##....
....##.....##...##.##...###...##.##....##.....###...###...##.##...###...##....
....##.....##..##...##..####..##.##...........####.####..##...##..####..##....
....#########.##.....##.##.##.##.##...####....##.###.##.##.....##.##.##.##....
....##.....##.#########.##..####.##....##.....##.....##.#########.##..####....
....##.....##.##.....##.##...###.##....##.....##.....##.##.....##.##...###....
....##.....##.##.....##.##....##..######......##.....##.##.....##.##....##....
..............................................................................
_____
| / |
|/
|
|
|
__|________
_ _ _ _ _
Guess a letter:
Feb 10, 2013 at 6:03am UTC
@Macrowav3
Try putting a break ;
after each theword = "..."
, otherwise, the checking keeps dropping down, and theword changes with it, till it gets to the last word, and that is the one it stops at.
Feb 10, 2013 at 3:21pm UTC
Thanks! Its working perfectly now!
Topic archived. No new replies allowed.