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
#include<iostream>
#include<string>
usingnamespace 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);
elsereturn(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!