unresolved external error

Hey guys,
I'm about 2 months into c++ and I've written a fairly simply code for the High Low Game in which the system generates a number and the user has to guess the number.
However, fatal error LNK1120: 1 unresolved externals keeps coming up.
Any help would be appreciated.
Thanks in advance

Syntax:
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
//High Lo Number Guessing Game
#include <iostream>
#include "time.h"

using namespace std;

void main ()
{
	while (1)
	{
	int num, guess;
	srand(time(0));
	num = rand () % 100 + 1;
	
	cout << "Guess a number (1-100): " << endl;
	
	do
	{
		cin >> guess;
		if (guess > num)
			cout << "Too high" << endl;
		if (guess < num) 
			cout << "Too Low" << endl;
		if (guess == num) 
		{
			cout << "Good Job. You guessed it" << endl;
			break;
		}
			
	}
	while (guess != num)
	loop:
		cout << " Would you like to play again? (y or n)" << endl;
	char ans;
		cin << ans;
		if ( ans== y)
			continue;
		else if (ans == n)
		{cout << "Thankyou for playing! /nGood Bye" << endl;
		break;}
		else if (ans != y && ans != n)
		{	cout << "Invalid Entry/nEnter y for yes or n for no" << endl;
			goto loop;
		}
		
		
	}

}
What exactly is the error? (hang on - let me guess)

To use rand(), you need to have #include <cstdlib> in there.

Also, it should be int main(), not void main().
And since it's C++ (not C), you can (should?) also just put #include <ctime> , rather than #include "time.h" (but nothing will happen in this case if you just leave it as it is).
Don't give us the abridged version of the error. Give us the entire error. You left out the most important part of the error message (ie: what symbol the linker is failing to find) so it's impossible for us to help you solve it.

Also, there are a lot more problems with this than just the linker error. In addition to what "long double main" said....

- don't call srand before every call to rand. srand should be called exactly ONCE. Don't call it for every random number.

- Your while loop on line 31 is missing an open brace.

- why do you have a loop: label and matching goto? You're already inside a while loop! Just let the while loop keep the loop going -- that's what it's there for.

- I don't see where you declared y or n. Those are variables, and you are treating them like literals. Just naming a variable y doesn't mean it represents the character 'y'. It be like saying int five; and expecting that variable to equal 5 just because of its name. It doesn't work that way. Variable names mean nothing to the computer. You want to use a literal 'y' and 'n':

if(ans == 'y') // <- note the apostrophes

- on line 35 you are using << with cin. You meant to use >>
Last edited on
- Your while loop on line 31 is missing an open brace.


That's actually the tail end of his do-while loop which is missing a ;

I'm guessing his crappy formatting is why he felt it necessary to use a label and goto statement.

I don't know how you get to linker errors, since your code doesn't compile.

Thanks guys
Topic archived. No new replies allowed.