Basic c++ Hangman game

the simplest hangman game evar

Page 1 of 1

6 Replies - 14722 Views - Last Post: 20 January 2010 - 08:15 PM Rate Topic: ***-- 2 Votes

#1 protomanv9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-January 10

Basic c++ Hangman game

Posted 20 January 2010 - 06:03 PM

Hi, I am new to this forum and in need of some help. I'm in a c++ class at my High School and need to make a hangman game. I can't use functions or strings or anything more then I already learned. I started to code the main area, but I can't get the "newword" to match "word", therefor you can never win.. Can someone help me out without changing a lot of the code?

EDIT: The real problem was that I was stuck in the while loop

////////////////////////////////////////////
//										//
// *Created by MMMMMMMEEEEEE			  //
//										//
////////////////////////////////////////////

#include<iostream.h>
#include<conio.h>

int main()
{
   int lifes=0;
   char letter;
   int nope;
   char word[20]={0};
   char newword[20]={0};

   cout<<"Write a word: ";
   cin>>word;

   while ( lifes <=5 && newword != word) 
   {
	nope=0;
	cout<<endl<<"Guess a letter: ";
	cin>>letter;
				
	for(int n=0; n<21; n++) {
	
	if(word[n] == letter) {
	word[n]=newword[n];
	nope=1; } }
	
	if(nope<1) {
	lifes=lifes+1;
	cout<<"Sorry please try again."<<endl; }
   }
	   
   if (lifes>5) {
   cout<<endl<<"You lose"; }
   else if (newword == word) {
   cout<<endl<<"You win!"; }

   getch();
   return 0;
}


Sorry that my structure isn't very nice.

/\/\/\/\/\/\/\/\/\ EDIT: Thanks for all the help!! It works! /\/\/\/\/\/\/\/\/\

////////////////////////////////////////////
//										//
// *Created by MMMMMMMEEEEEE			  //
//										//
////////////////////////////////////////////

#include<iostream.h>
#include<conio.h>

int main()
{
   int lifes=0;
   char letter;
   int nope;
   char word[20]={0};
   char newword[20]={0};

   cout<<"Write a word: ";
   cin>>word;

   while ( lifes <=5 && strcmp(newword, word) != 0)
 
   {
	nope=0;
	cout<<endl<<"Guess a letter: ";
	cin>>letter;
				
	for(int n=0; n<20; n++) {
	
	if(word[n] == letter) {
	newword[n]=word[n];
	nope=1; } }
	
	if(nope<1) {
	lifes=lifes+1;
	cout<<"Sorry please try again."<<endl; }
   }
	   
   if (lifes>5) {
   cout<<endl<<"You lose"; }
   else {
   cout<<endl<<"You win!"; }

   getch();
   return 0;
}

This post has been edited by protomanv9: 20 January 2010 - 09:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Basic c++ Hangman game

#2 protomanv9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-January 10

Re: Basic c++ Hangman game

Posted 20 January 2010 - 06:26 PM

wrong posstt

This post has been edited by protomanv9: 20 January 2010 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 sonicpp  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 58
  • Joined: 08-January 10

Re: Basic c++ Hangman game

Posted 20 January 2010 - 06:56 PM

Your for loop is going too far.
for(int n=0; n<21; n++) {
// should be
for(int n=0; n<20; n++) {



Not sure if this is the problem but it is a problem
Was This Post Helpful? 0
  • +
  • -

#4 sonicpp  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 58
  • Joined: 08-January 10

Re: Basic c++ Hangman game

Posted 20 January 2010 - 07:22 PM

Also you are trying to check char arrays with an =. You need to use strcmp(char* str1, char* str2)
Example.... using your code again.
while ( lifes <=5 && newword != word)
// should be
while ( lifes <=5 && strcmp(newword, word) != 0)


Also your win check at the end can be simply...
   if (lifes>5) {
	cout<<endl<<"You lose"; 
	return 0;
   }
   
   cout<<endl<<"You win!" << endl;

   cin >>getch();
   return 0;


But for the most part you did a nice job of trying... something some people on this site don't.
Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Basic c++ Hangman game

Posted 20 January 2010 - 07:43 PM

Another issue is that you have the assignment to the new word backwards. Instead of:
	if(word[n] == letter) {
	word[n]=newword[n];
	nope=1; } }


it should be:
	if(word[n] == letter) {
	newword[n]=word[n];
	nope=1; } }


Was This Post Helpful? 1
  • +
  • -

#6 protomanv9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-January 10

Re: Basic c++ Hangman game

Posted 20 January 2010 - 08:00 PM

Thanks for all the help guys! It works perfectly now, this is what I did:

////////////////////////////////////////////
//										//
// *Created by MMMMMMMEEEEEE			  //
//										//
////////////////////////////////////////////

#include<iostream.h>
#include<conio.h>

int main()
{
   int lifes=0;
   char letter;
   int nope;
   char word[20]={0};
   char newword[20]={0};

   cout<<"Write a word: ";
   cin>>word;

   while ( lifes <=5 && strcmp(newword, word) != 0)
 
   {
	nope=0;
	cout<<endl<<"Guess a letter: ";
	cin>>letter;
				
	for(int n=0; n<20; n++) {
	
	if(word[n] == letter) {
	newword[n]=word[n];
	nope=1; } }
	
	if(nope<1) {
	lifes=lifes+1;
	cout<<"Sorry please try again."<<endl; }
   }
	   
   if (lifes>5) {
   cout<<endl<<"You lose"; }
   else {
   cout<<endl<<"You win!"; }

   getch();
   return 0;
}

Was This Post Helpful? 0
  • +
  • -

#7 sonicpp  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 58
  • Joined: 08-January 10

Re: Basic c++ Hangman game

Posted 20 January 2010 - 08:15 PM

Looks good... now you can work on the hangman output. You seem like a smart guy I'm sure that will be no problem. Sorry I didn't mention the newword[n] = word[n]... I brought your code in and got it to work on my machine then completely forgot that KEY issue. Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1