Fatal Error Issue

Hey Guys, First of all thanks for looking, basically I'm getting a fatal error from the code below and essentially what I need is to know what is needed to fix the error and have it so that the letter which is inputted by the user will be compared to the chosen word from the list and then if correct change the '*' the letter? (Preferably in dummy terms as I'm very new to c++ lol)

Thanks so much :)

Last edited on
Please use the code tags

You need to escape the backslashes.

 
cout<<(" | | | | / \\ | \\ | |/ ___| \\/ | / \\ | \\ | | \n");	



edit:

also - why do you have brackets around your HANGMAN?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(void)
{

{  <-- THIS ONE
cout<<(" _ _ _ _ _ ____ __ __ _ _ _ \n"); 
cout<<(" | | | | / \ | \ | |/ ___| \/ | / \ | \ | | \n");	
cout<<(" | |_| | / _ \ | \| | | _| |\/| | / _ \ | \| | \n");	
cout<<(" | _ |/ ___ \| |\ | |_| | | | |/ ___ \| |\ | \n");	
cout<<(" |_| |_/_/ \_\_| \_|\____|_| |_/_/ \_\_| \_| \n");	
cout<<("\n");
cout<<("\n");
cout<<("\n");
cout<<("\n");
cout<<(" Welcome To Hangman!\n\n Press space bar to continue...\n");
} <-- THIS ONE TOO
Last edited on
hmmm thanks for pointing that out, I'm not at all sure - any ideas how I can make this code become working?
Cheers
You have to escape the backslashes, the compiler thinks a special character will follow like '\n' '\t' . But if i fix that it compiles for me. What are the errors you are getting and with what compiler?

1
2
3
cout << "\"
// Needs to become 
cout << "\\" 
I can't seem to see exactly what back slashes it is that need to be changed, would it be possible for you to paste the source code of the version you are finding that is compiling ok?

Thanks very much!!
here is the build error I am having:

1
2
3
4
 1>Hangman.obj : error LNK2019: unresolved external symbol "int __cdecl test_letter(char)" (?test_letter@@YAHD@Z) referenced in function _main
1>J:\Uni\Visual Studio 2010\Projects\Hangman\Debug\Hangman.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
This works for me, with gcc (though I commented out conio).

Notice the HANGMAN - I double backslashed everything that was a single backslash.


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
#include <iostream>
#include <stdio.h>
#include<conio.h>
#include <ctype.h>
#include <time.h>

using namespace std;

char wordbank[30][30]={"book","cook","Hook","took","wookie","snookie","dog","cat","house","tyranasouras rex","dolphin","jungle","phone","drum","bomb","airport","plane","guess","mess","chess","less","locc-ness","jess","bulge","smulge","indulge","Bin","Ladder","Sausage","chair"};
int word_length;
int lives = 6;
char display_word[20];
char secret_word[20];
char guess_word[20];
char word_found;
int i;
int x =0;
char hidden_word[20];
int count = 0;
char guess;
int test;
int test_letter(char guess);


int main(void)
{

  
	  cout<<"  _   _    _    _   _  ____ __  __    _    _   _  \n";    
          cout<<" | | | |  / \\  | \\ | |/ ___|  \\/  |  / \\  | \\ | | \n";	 
	  cout<<" | |_| | / _ \\ |  \\| | |  _| |\\/| | / _ \\ |  \\| | \n";	  
	  cout<<" |  _  |/ ___ \\| |\\  | |_| | |  | |/ ___ \\| |\\  | \n";	  
	  cout<<" |_| |_/_/   \\_\\_| \\_|\\____|_|  |_/_/   \\_\\_| \\_| \n";	  
	  cout<<"\n";
	  cout<<"\n";
	  cout<<"\n";
	  cout<<"\n";
	  cout<<" Welcome To Hangman!\n\n Press space bar to continue...\n";
  

		cout<<"+---+\n      |\n      |\n      |\n      |\n      |\n=========\nPlease make your first guess, choose a letter.\n";
		

	int secret_word, secret_word_Len,i;
    srand ( time(NULL) );
    secret_word = rand() % 30 ; //Chooses a random word from the word bank  
    strcpy(&hidden_word[0],&wordbank[secret_word][0]);
    secret_word_Len=strlen(hidden_word);
    cout<<"Your chosen word has "<<secret_word_Len<<" Letters\n";
	cout<<"You Have "<< lives<<" Lives Remaining\n";
    cout<<"\n"; 
	cout<<"Enter a Guess\n";
	cout << "\n\n" << secret_word_Len;

    for(i=0;i<secret_word_Len;i++)
	{
      cout<<"*";
	}

	{	
    cout<<"\n"<<&hidden_word[0]<<"\n";
    cin.get();
  return 0;
		}
	while ( ! (cin >> guess))
	{
	
		test=test_letter(guess);
	if(test==1)
	{
		cout<<"Correct Guess\n";
	}


		
	   if (guess == hidden_word[i])
	   {
         display_word[i]= (guess);
		}
	}

	cin.get();
	cin.get();
}
Hey again,


The program is a C++ hangman game (.cpp file)

when compiling this I am having the following issue (even with using your backslash solution) :

1
2
3
4
 1>Hangman.obj : error LNK2019: unresolved external symbol "int __cdecl test_letter(char)" (?test_letter@@YAHD@Z) referenced in function _main
1>J:\Uni\Visual Studio 2010\Projects\Hangman\Debug\Hangman.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.



Many thanks
Last edited on
That would be on line 22, you declare

 
int test_letter(char guess);


This is a function declaration, which is not defined in your code. Maybe you have another function that wasn't posted? Or you have to define the function.

http://www.cplusplus.com/doc/tutorial/functions/


Helppppp xD
Last edited on
Sure I will help. I think there are some things you need to clean up though.


Take a look at:
Line 60 and 64 have random brackets, what are these for?
Line 62 has cin.get(), but what is it doing with the character it gets?
Line 63 has a return statement, which ends execution, is that what you want there?
Line 65 has a while statement, but you are negating the input. Not sure that is what you intended


So you have all the things for the function, but you need to decide how it works. Since you are learning about functions, something important is variable scope, something to look up, as its going to come in handy here :)



Anyways test_letter should know the character the user guessed, the secret word and preferably the length of the secret word. You would want to loop through the secret word and see if the guess is in the secret word.

Have the function return the position in the secret word array, which the guess was found, or some arbitrary number (-1 is used a lot) to tell your main function it is not in the secret word.


1
2
3
4
5
6
7
8
9
10
int test_letter(char guess, secret_word[20], secret_word_Len)
{
  Loop through secret_word
  {
    if guess equals secret_word[i]
      return place
  }
// If it is not found, return -1 so you know it wasn't found.
  return -1
}


Or a better way would be to include with the secret word, guess character and length of secret word and display_word, which i assume represents the users correct guesses, and add the guess to the correct place in the display_word array.

Let me know if that helps

Last edited on
My main comment is the use of C approach in a C++ program.

You use char array, when you could use C++ strings, and you have C functions like strcpy when you could just assign strings.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>

using std::string;

char A[30];
char B[30];

strcpy (A, B);

string StrA = "Hello ";
string StrB  = "World";

string StrC = StrB;

string Output = StrA + StrC;

std::cout << Output << std::endl;


You can still access individual chars in a C++ string.

Read all about in this tutorial here:

http://www.cprogramming.com/tutorial/string.html


and some reference material here:

http://www.cplusplus.com/reference/string/string/


There is a lot of reference material on this site - have a read.
Hey deviants, thanks so much for the help, I understand what you are saying however I'm having difficulties implementing it so that it compiles correctly, does the code you pasted above work for you??
Cheers
...
Last edited on
you could still use strings and the function c_str()
;)
Topic archived. No new replies allowed.