I was wondering is the display function wrong when I call the search function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I was wondering is the display function wrong when I call the search function?

When it get the answer for the question in search function my compiler still print out both of the function i don’t know why int search(string arr[ROW][COLUMN], string STR) { for (int i=0; i<ROW; i++) { if (arr[i][0]==STR) { cout <<"Answer for the question: " <<arr[i][1] << endl; return i; } } return -1; } void display (string STR) { string arr[ROW][COLUMN]; int match= search(arr, STR); if (match == -1) { cout << "Please enter a choice from below\n"; //code for choice } }

4th Dec 2020, 12:21 PM
Skylar Yang
Skylar Yang - avatar
5 Answers
+ 1
You declared arr[][] as int array and function defination has string arr[][], it should display error.. And what are the values to array, not mentioned there, how you storing.. And your while loop is infinite loop, no exit condition. Instead use if(match== -1){ .. }
4th Dec 2020, 1:57 PM
Jayakrishna 🇮🇳
+ 1
Assign the values to string array arr. Then call search function. Otherwise it is empty array and return -1 only... Edit : Skylar Yang that row and column index values.. What and where is the values for arr[0][0],arr[0][1], arr[1][0],arr[1][1], ....... ....... arr[99][0],arr[99][1]
4th Dec 2020, 8:47 PM
Jayakrishna 🇮🇳
0
You didn't initialize the arr in display() with size.
4th Dec 2020, 1:57 PM
你知道規則,我也是
你知道規則,我也是 - avatar
0
oops sorry i write the wrong code I updated the code again
4th Dec 2020, 2:12 PM
Skylar Yang
Skylar Yang - avatar
0
I use const int ROW=100 and const int COLUMN =2 in declaration Jayakrishna🇮🇳 arr[0][0] to arr[99][0] will be the input file for question arr[0][1] to arr[99][1] will be the input file for answer
4th Dec 2020, 8:50 PM
Skylar Yang
Skylar Yang - avatar