0
#include<iostream>
#include<conio.h>
using namespace std;
struct Infor{
       char name[21];

       }infot[4];
int main()
{
    infot[0].name="PROGRAM 1";
    cout<<infot[0].name;
    getch();
    return 0;
}

The code is used to initialize a string into a structure containing a char variable.

1 Answers1

3

You should use strcpy() to copy c-style strings.

#include<iostream>
#include<cstring>
using namespace std;
struct Infor{
       char name[21];
       int acn;
       int pin;
       int balance;
       }infot[4];
int main()
{
    strcpy(infot[0].name, "PROGRAM 1");
    cout<<infot[0].name;
    return 0;
}

Alternatively, std::string may be useful.

#include<iostream>
#include<string>
using namespace std;
struct Infor{
       string name;
       int acn;
       int pin;
       int balance;
       }infot[4];
int main()
{
    infot[0].name = "PROGRAM 1";
    cout<<infot[0].name;
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70