Please Help Me!

This is a discussion on Please Help Me! within the C++ Programming forums, part of the General Programming Boards category; I need some help with my code. How can I allow the player to guess the word if they'd want ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    Can someone help me with this?

    I need some help with my code.

    How can I allow the player to guess the word if they'd want to, instead of going through and guessing letters when they already know the word.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <string>
    #include <cmath>
    
    using namespace std;
    
    void type_of_word(char f);
    
    
    int main()
    {   
    		char c, ch, ch1, ch2;
    
    		char word[50];
    
    		char word1[25];
    
    		int l, i, next_guess, n, m, x;
    
    		do
    
    		{  
    			do
    
    			{
    
    			cout<<"\n\n\n\t********************************************************\n";
    
    			cout<<"\t********************* WoRd GuEsSeR *********************\n";
    
    			cout<<"\t********************************************************\n\n\n";
    
    			cout<<"\n \t(E) Enter your own word\n\n \t(C) Computer Chosen Word @ Random\n\n \t(Q) Quit\n\n\n Enter your choice: ";
    			
    			cin>> ch2;
    
    			}
    		
    		while	(ch2 != 'C' && ch2 != 'c' && ch2 != 'E' && ch2 != 'e' && ch2 != 'Q' && ch2 != 'q'&& ch2 != 'A' && ch2 != 'a');
    		
    		if	(ch2 == 'Q' || ch2=='q')  exit (0);
    
    		if	(ch2 == 'C' || ch2=='c')
    
    		{
    			ifstream fin("words.txt");
    			
    			if	(!fin) 
    			
    			{ 
    			
    			cout<<"\n\nYou are missing a file called words.txt\n\n"; 
    				
    			return 0;
    			
    			}
    			
    			for (i=0;!fin.eof();i++)   fin.getline(word,25);
    			
    			fin.close();
    
    			do 
    			
    			{
    			
    			x=rand();
    			
    			}
    			
    			while(x>i || x<0);
    
    			ifstream finn("words.txt");
    			
    			for (i=0;!finn.eof();i++)
    
    			{finn>>c; finn.getline(word,25); if (x==i) break;}
    
    			finn.close();
    		}
    
    		if	(ch2 == 'E' || ch2=='e')
    		
    		{
    			
    		cin.get();
    
    		cout<<"\tType the word :  ";
    
    		cin.getline (word, 25);
    		
    		}
    		
    		if	(ch2 == 'E' || ch2=='e' || ch2 == 'C' || ch2=='c')
    		
    		{
    			l=strlen(word);
    
    			char choosen[50]="\0";
    
    			n=0; 
    			
    			m=0;
    
    		for	(i=0; i<=24; i++)
    		{
    			if (word[i]=='\0') {word1[i]='\0'; break;}
    
    			if (word[i]==' ')  {word1[i]=' ';  n++;}
    
    			if (word[i]!=' ')  word1[i]='-';
    		}
    
    		next_guess=l+2-n;     //Guess left, 2 more
    		
    		do
    		{
    		
    			there:  type_of_word(c);
    			
    			if (m!=0)  cout<<"\n\n\t\t\tChoosen letters : "<<choosen<<"\n";
    			
    			cout<<"\n\n\n\t\t\t      "<<word1<<"\n\n\nYou have "<<next_guess<< " guesses left, choose a letter : ";
    			
    			cin>>ch; cin.get();
    			
    			for (i=0;i<25;i++) 
    				
    			if (choosen[i]==ch) 
    			
    			{
    			
    			cout<<"\a\t\t     !!You have choosen "<<ch<<" already!!\n"; goto there;
    			
    			}
    			
    			next_guess--; 
    			
    			choosen [m]=ch; 
    			
    			choosen [m+1]=',';
    			
    			m+=2;
    
    			for (i=0;i<=49;i++)    
    				
    			if (word[i]==ch || word[i]==ch+32 || word[i]==ch-32) word1[i]=ch;
    			
    			if (!strcmpi (word1,word)) {cout<<"\n\t\t\t      "<<strupr(word)<<"\n\n\t\t\tCongratulations, You have won!\n"; break;}
    
    			}
    			
    		while(next_guess>0 || !strcmpi (word1,word));
    
    
    		if (strcmpi (word1,word))  cout<<"\nSorry, maybe next time.\n\nThe word was : "<<strupr(word)<<endl;
    		
    		}
    
    		cout<<"\nWould you like to play again? [Y or N] : ";
    
    		cin>>ch1;  cin.get();
    
    		}
    		
    			while (ch1=='y' || ch1=='Y');
    
    			system("PAUSE");
    			
    			return 0;
    }
    
    		void type_of_word(char f)
    
    		{   if (f=='p') cout<<"\t\t\t\tPROGRAMMING";
    			if (f=='s') cout<<"\t\t\t\tSPORT";
    			if (f=='t') cout<<"\t\t\t\tTV SHOW";
    		}
    Last edited by unejam2005; 12-11-2005 at 03:58 PM. Reason: I don't need help with that portion of the code anymore.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Did you say you delete the following line and it gave you error msg?
    Code:
    there:  type_of_word(c);
    What's the error msg? Pointing to what line?

    alternative to using the strcmpi, and the strlen?
    Use the string class from STL and you can do something like the following
    Code:
    string word1 = "hello";
    string word2 = "hell0";
    
    if (word1 == word2) {
      echo << "same words";
    }
    else {
      echo << "different words";  
    }
    alternative to strlen
    Code:
    string word = "hello world";
    size_type length = word.size();
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21