-4

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?

user3599755
  • 93
  • 3
  • 11
  • 2
    You have an array of five arrays of two characters, meaning you can only store five *one-character strings*. – Some programmer dude May 17 '14 at 19:26
  • @JoachimPileborg Thank you for the guidance, yes now I understood the logic. Is there any way to store the capt and stat array in other array. Basically I need to check and display the capital name if user enter state name. – user3599755 May 17 '14 at 19:30
  • 3
    What the hell book are you learning from?? It must be decades old. This isn't even valid C++. – Edward Strange May 17 '14 at 19:30
  • 2
    Get a decent C++ tutorial and use `std::string`. And yes, get a recent compiler, there are several free ones available. – Ulrich Eckhardt May 17 '14 at 19:30
  • Dear all experts I am helpless i have to use this compiler only it is recommended by school. I also use codeblock but that is not acceptab;e by school – user3599755 May 17 '14 at 19:32

2 Answers2

3

The expression capt_stat[i][j] is a single char and not a string, which is expected by strcpy.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

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 
HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
  • Thanx for considering my problem. but what about loop how I will construct the loop for this array? – user3599755 May 17 '14 at 19:35
  • Yes done but now question is for list of state and capital also I need 2D array like - capt[5][20] for this I used `cin.getline(capt[i],20);` and again getting same error on same line. – user3599755 May 17 '14 at 19:49
  • Bandypadhyay Thank you for all support. Work is done. but if will be thankful if you can tell me the logic behind using 3rd [] in capt_stat array as you told. – user3599755 May 17 '14 at 20:01
  • You can think like this: `capt_stat` contains 5 rows, 2 columns, and each cell contains 20 characters. So it is declared like that. – HelloWorld123456789 May 17 '14 at 20:03
  • basically what i know this type of declaration means 3D array. So can I say that we can use 3D array for storing the name of 5 states and 5 captials of maximum 20 characters. – user3599755 May 17 '14 at 20:08