Reputation: 13
I've been trying to pass this array to the function but i keep on getting, error C2664: 'correctans' : cannot convert parameter 2 from 'std::string [3][3]' to 'std::string **' , don;t mind the silly questions in the code its just random for testing.
code:
#include <iostream>
#include <string>
using namespace std;
int correctans(string *arr1, string **arr2, int *arr3, int questions, int choices)
{
int count=0;
int ans;
for(int i=0; i<questions; i++)
{
cout << "Question #" << i+1;
cout << arr1[i] << endl;
for(int j=0; j<choices; j++)
cout << j+1 << arr2[i][j] << " ";
cout << "your answer:";
cin >> ans;
if(ans==arr3[i])
count++;
}
return count;
}
int main()
{
int correct;
string Questions[3]={"HowAreYou", "HowManyHandsDoYouHave", "AreYouCrazyOrCrazy"};
string Choices[3][3]={{"Banana", "Peanut", "Fine"},{"Five", "Two", "One"},{"I'mCrazy", "I'mCrazyBanana", "I'mDoubleCrazy"}};
int Answers[3]={3, 2, 3};
correct=correctans(Questions, Choices, Answers, 3, 3);
cout << "You have " << correct << " correct answers" <<endl;
return 0;
}
Upvotes: 1
Views: 171
Reputation: 1430
Well, as compiler said std::string [3][3]
can not be converted to std::string **
.
You can try this
int correctans(string *arr1, string (* arr2)[ 3 ], int *arr3, int questions, int choices)
or this
int correctans(string *arr1, string arr2[][ 3 ], int *arr3, int questions, int choices)
But better solution is to use std::vector
.
Upvotes: 0
Reputation: 6021
Passing multidimensional arrays can get very confusing. I recommend creating a singlepointer to the start of your array, and passing that pointer:
#include <iostream>
#include <string>
using namespace std;
int correctans(string *arr1, string *arr2, int *arr3, int questions, int choices)
{
int count=0;
int ans;
for(int i=0; i<questions; i++)
{
cout << "Question #" << i+1;
cout << arr1[i] << endl;
for(int j=0; j<choices; j++)
cout << j+1 << arr2[i][j] << " ";
cout << "your answer:";
cin >> ans;
if(ans==arr3[i])
count++;
}
return count;
}
int main()
{
int correct;
string Questions[3]={"HowAreYou", "HowManyHandsDoYouHave", "AreYouCrazyOrCrazy"};
string Choices[3][3]={{"Banana", "Peanut", "Fine"},{"Five", "Two", "One"},{"I'mCrazy", "I'mCrazyBanana", "I'mDoubleCrazy"}};
int Answers[3]={3, 2, 3};
string* choicesPtr=&Choices[0][0];
correct=correctans(Questions, choicesPtr, Answers, 3, 3);
cout << "You have " << correct << " correct answers" <<endl;
return 0;
}
This code compiles and executes.
Upvotes: 1
Reputation: 2865
If I remember correctly you can use something like 'string [][] & rArray'
(or exactly: string [3][3] & rArray), of course you should pass the actual dimensions as parameters as well.
The compiler accepted this:
string arr2[3][3]
as parameter. But i would try to improve passing a pointer instead of copying an array by value. Since you are using stl::string you could also try vector< vector< string > >.
Upvotes: 0
Reputation: 871
Here you go
int correctans(string *arr1, string (&arr2)[3][3], int *arr3, int questions, int choices)`
Upvotes: 1