Search code examples
c++turbo-c++turbo-c

Getting error Cannot convert 'int' to 'char *'


I want to store name of states and their capital in 2D array like this

State     | Capital
----------|--------
Bihar     | Patna
----------|--------
Jharkhand | Ranchi
----------|--------
Gujarat   | Gandhinagar

I tried to do this by code below using Turbo C++

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
   char capt[20];
   char stat[20];
   char capt_stat[5][2];
   for(int i=0;i<5;i++)
   {
      cin>>capt;
      cin>>stat;
      for(int j=0;j<1;j++)
      {
     strcpy(capt_stat[i][j],stat);
     strcpy(capt_stat[i][j+1],capt);
      }
   }
   for(int i=0;i<5;i++)
   {
      for(int j=0;j<1;j++)
      {
         cout<< capt_stat[i][j]<<"  "<<capt_stat[i][j+1];
      }
      cout<<endl;
   }
}

but getting error Cannot convert 'int' to 'char *' in line 18 that is -

strcpy(capt_stat[i][j],stat);

Kindly guide me what is wrong?


Solution

  • You want capt_stat[i][j] to be of type char *.

    Declare as

    char capt_stat[5][2][20]; // 20 is the maximum length of each string