Unresolved externals problem

I'm doing this program for class and it's not working. I'm getting the unresolved external message. Any help would be appreciated

Error 1 error LNK2019: unresolved external symbol "int __cdecl getans(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?getans@@YAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main c:\Users\robot arena\documents\visual studio 2013\Projects\Final Project 2\Final Project 2\Source.obj Final Project 2


#include<iostream>
#include<string>

using namespace std;
int getans(string ans);


int getans(string ans[])
{
int cor = 0, incor = 0;

string answer[5] = { "b", "a", "b", "c", "a" };

if (ans == answer)
cor++;
else
incor++;

return cor, incor;
}

int main() {

string ans;
int cor = 0, incor = 0;

cout << "1. Which of these Pokemon is a fairy type? \n a. Chansey \n b. Togepi \n c. Skitty \n";
cin >> ans[getans(ans)];

cout << "2. Which of these Naruto characters does NOT have a kekei genkai? \n a. Jiriya \n b. Kimimaru \n c. Sasuke \n";
cin >> ans[getans(ans)];

cout << "3. What does Kenshin say when he's confused? \n a. Uwah? \n b. oro? \n c. mowa? \n";
cin >> ans[getans(ans)];

cout << "4. What's the most annoying radio commercial ever? \n a. Geico \n b. Other insurance company \n c. Kars for kids \n";

cin >> ans[getans(ans)];

cout << "5. How do you defeat Primal Groudon? \n a. ground type moves \n b. water type moves \n c. grass type moves \n";

cin >> ans[getans(ans)];

cout << "You got " << cor << " answers right and " << incor << " answers wrong ";


system("pause");

}
are you using any extra libraries and forgot to include them in your project?
Last edited on
I don't think you need to put the indexer [] in your function definition...

int getans(string ans[]) ... can just be, int getans(string ans)

...

also, returning two variables at the end of the function is bad news.

...

My suggestion would be to read in a character variable " char answer; cin >> answer; " Then send that one character to the function and compare it against your array...which should probably be a character array " char answers[5] = { ... } " .

then you might need to send the question number to the function also... " int getans( char ans, int questionNum ) " ... this way you can use questionNum as your index for the character array " char answer[5] = { "b", "a", "b", "c", "a" }; " ...

and " if (ans == answers[questionNum]) { correct++; } else {incorrect++;} " but since those variables are local only to that function you might need to make correct and incorrect variables global
Can you please show me the fixed code?

I still can't seem to figure out how to fix it from what you said.

Id' really appreciate it.
why are you trying to use this line...

cin >> ans[getans(ans)]; ???

what are you trying to have happen there?

If the assignment isn't specific about the use of strings then you could use this

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
#include<iostream>
#include<string>

using namespace std;

int getans(char ans, int num);

int main() 
{
	char ans;
	int cor = 0, incor = 0;

	cout << "1. Which of these Pokemon is a fairy type? \n a. Chansey \n b. Togepi \n c. Skitty \n";
	cin >> ans;
	if(getans(ans,0))
		cor++;
	else
		incor++;
	cout << "2. Which of these Naruto characters does NOT have a kekei genkai? \n a. Jiriya \n b. Kimimaru \n c. Sasuke \n";
	cin >> ans;
	if(getans(ans,1))
		cor++;
	else
		incor++;
	cout << "3. What does Kenshin say when he's confused? \n a. Uwah? \n b. oro? \n c. mowa? \n";
	cin >> ans;
	if(getans(ans,2))
		cor++;
	else
		incor++;
	cout << "4. What's the most annoying radio commercial ever? \n a. Geico \n b. Other insurance company \n c. Kars for kids \n";
	cin >> ans;
	if(getans(ans,3))
		cor++;
	else
		incor++;
	cout << "5. How do you defeat Primal Groudon? \n a. ground type moves \n b. water type moves \n c. grass type moves \n";
	cin >> ans;
	if(getans(ans,4))
		cor++;
	else
		incor++;
	cout << "You got " << cor << " answers right and " << incor << " answers wrong ";
	
	system("pause");
}



int getans(char ans,int num)
{
	char answer[5] = { 'b','a','b','c','a' };

	if (ans == answer[num])
		return(1);
	else
		return(0);
}
Last edited on
Thank you so much! That really helped me out! Can you please do me one more favor?

You see, I'll definitely get more credit if I incorporate reading a file in this program.

I'm so close to getting this right, but it says

Function does not take 2 arguments. I'd really like to know how to fix that. If this code works while reading a file, that would be fantastic!

Here is the code:


#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int getans(char ans, int num, char corans);

int main()
{
char ans;
int cor = 0, incor = 0;



cout << "1. Which of these Pokemon is a fairy type? \n a. Chansey \n b. Togepi \n c. Skitty \n";
cin >> ans;
if (getans(ans, 0))
cor++;

else

incor++;

if (getans(ans, 0))
cout << "All right! You got the right answer! \n";
else
cout << "Sorry. You got the wrong answer.....\n";

cout << "\n 2. Which of these Naruto characters does NOT have a kekei genkai? \n a. Jiriya \n b. Kimimaru \n c. Sasuke \n";
cin >> ans;
if (getans(ans, 1))
cor++;
else
incor++;

if (getans(ans, 1))
cout << "All right! You got the right answer! \n";
else
cout << "Sorry. You got the wrong answer.....\n";

cout << "\n 3. What does Kenshin say when he's confused? \n a. Uwah? \n b. oro? \n c. mowa? \n";
cin >> ans;
if (getans(ans, 2))
cor++;
else
incor++;

if (getans(ans, 2))
cout << "All right! You got the right answer! \n";
else
cout << "Sorry. You got the wrong answer.....\n";

cout << "\n 4. Which of these characters is NOT from Earthbound? \n a. Lucas \n b. Porky \n c. Bugzy \n";
cin >> ans;
if (getans(ans, 3))
cor++;
else
incor++;

if (getans(ans, 3))
cout << "All right! You got the right answer! \n";
else
cout << "Sorry. You got the wrong answer.....\n";


cout << "\n 5. How do you defeat Primal Groudon? \n a. ground type moves \n b. water type moves \n c. grass type moves \n";
cin >> ans;
if (getans(ans, 4))
cor++;
else
incor++;

if (getans(ans, 4))
cout << "All right! You got the right answer! \n";
else
cout << "Sorry. You got the wrong answer.....\n";

cout << "\n You got " << cor << " answers right and " << incor << " answers wrong \n";

if (cor <= 1)
cout << "You need more nerd knowledge! \n";
else if (cor = 3)

cout << "Pretty decent score. Good job. \n";
else if (cor = 5)
cout << "WOW! You sure know your stuff! Congrats on the perfect score! \n";

system("pause");
}



int getans(char ans, int num)


{
char corans;
int count = 0;

ifstream quiz;
quiz.open("Removable Disk (I:)/quizfile.txt");
if (quiz.fail())
cout << "File error\n" << endl;

else {


while (!quiz.eof()) {
quiz >> corans;
count++;

char answer[5] = { corans };

if (ans == answer[num])
return(1);
else
return(0);

}
}
}

Really hope you'll be willing to help me out again! I'd really appreciate it!
Topic archived. No new replies allowed.