Thread: Help with section of code.

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

    Help with section of code.

    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";
    		}

  2. #2
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    you have a terrible syntax young man.
    as for guesing : if user knows the word, it wouldn't be a problem for him to "type" it letter by letter.
    When i say "type" i mean gues all the letters.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

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

    What do you mean I have terrible Syntax?

    I know that someone can type each letter one by one, but I would want the user to type in the word if he/she would want to guess. Can you elaborate on why my syntax is terrible, I would appreciate it.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    example of bad syntax
    Code:
    			if (choosen[i]==ch) 
    			
    			{
    			
    			cout<<"\a\t\t     !!You have choosen "<<ch<<" already!!\n"; goto there;
    			
    			}
    too many empty lines and no clear indentation
    try like this instead

    Code:
    	// some spaces between operators help readability
    	if (choosen[i] == ch) 
    	{ // no need for a blank line before this
    		// break up long lines
    		cout << "\a\t\t     !!You have choosen " 
    			<< ch << " already!!\n"; 
    		
    		// one statement on a line!!
    		goto there; // also think long and hard before using goto
    	}
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Need help with section of Code
    By Harkin1987 in forum C Programming
    Replies: 9
    Last Post: 08-27-2006, 01:55 PM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. How would you go back to a section of code?
    By XenoForce in forum C++ Programming
    Replies: 23
    Last Post: 10-20-2004, 10:51 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM