1

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;
}

4 Answers 4

1

Here you go

int correctans(string *arr1, string (&arr2)[3][3], int *arr3, int questions, int choices)`

Sign up to request clarification or add additional context in comments.

2 Comments

but I have a question what if i didn't know what is the size of the 2d array what can i use instead of [3][3] cause i tried changing the number just from the function but it didn't work i want to be able to set it up to any size is that possible?
@WildBegginner: you could make correctans template function. template<typename T, int SIZE> correctans( ..., string(&arr)[SIZE][SIZE], ... ). But if you don't know the size at the compile time, then it doesn't work.
1

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.

Comments

0

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 > >.

Comments

0

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.

2 Comments

the second one worked but it is not passed using a pointer. thanks though @kotlomoy
@WildBegginner Actually string arr2[][3] implicitly converted to string (* arr2)[3]. So both are identical for compiler. BTW the first one works now (I missed parentheses in first edition of my post)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.