I got some problem converting the char[] with "some word and bla bla bla"

how can i insert an char array with some word?
example:
MAX[counter].VacationLocation[10] = "LAKE GENEVA";
What is VacationLocation[10]? A character or a pointer-to-character (i.e. pointing to a c-string)?

Are you trying to append (concatenate) a string to another existing string? Can you explain more in detail of what you're actually trying to accomplish?

I suggest using std::strings instead, it will take care of so much for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example program
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> VacationLocations = { "KIVU", "LAKE VICTORIA", "PRIPYAT" };

    VacationLocations.push_back("LAKE GENEVA");

    VacationLocations[3] += " APPEND";
    
    for (size_t i = 0; i < VacationLocations.size(); i++)
    {
        std::cout << VacationLocations[i] << '\n'   ;
    }
}

KIVU
LAKE VICTORIA
PRIPYAT
LAKE GENEVA APPEND
Last edited on
Im trying to initialize a struct char array:
struct PurchaseInfo
{
char CustomerName[50];
char VacationLocation[30];
int SeatNumber;
char SeatAlphabet;
char CustomerReferenceCode[10];
char DepartDate[50];
char ReturnDate[50];
}MAX[100];

in one of my function, im trying to initialize MAX[counter].VacationLocation[30] with the string "the name of location". i tried to use strcpy but it shows error "invalid conversion from char* to const char or something like that.

i also tried to initialize the char MAX[counter].SeatAplhabet with 'A' using strcpy(MAX[counter].SeatAlphabet,'A') .......... but it shows error--
error: invalid conversion from 'char' to 'const char*' [-fpermissive]|
Are you allowed to change the struct?

I would suggest doing something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Example program
#include <iostream>
#include <string>

struct PurchaseInfo
{
    std::string CustomerName;
    std::string VacationLocation;
    int SeatNumber;
    char SeatAlphabet;
    std::string CustomerReferenceCode;
    std::string DepartDate;
    std::string ReturnDate;
} MAX[100];


int main()
{
    int counter = 0;
    
    MAX[counter].VacationLocation = "the name of location";
    MAX[counter].SeatAlphabet = 'A';
    
    std::cout << MAX[counter].VacationLocation << '\n';
    std::cout << MAX[counter].SeatAlphabet << '\n';
}


Also, you don't need to use strcpy to copy just a character. You can just do an assignment.

Also also, "MAX" is a really poor variable name. Why not "purchases"?
Last edited on
im sorry but in my university we are not allowed to use string, we need to use char array: eg. char[10] name;

Do you have any other suggestion?
Well that's dumb of your university.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Example program
#include <iostream>
#include <cstring>

struct PurchaseInfo
{
    char CustomerName[50];
    char VacationLocation[30];
    int SeatNumber;
    char SeatAlphabet;
    char CustomerReferenceCode[10];
    char DepartDate[50];
    char ReturnDate[50];
} MAX[100];


int main()
{
    int counter = 0;
    
    // strcpy(destination, source)
    std::strcpy(MAX[counter].VacationLocation, "the name of location");
    MAX[counter].SeatAlphabet = 'A';
    
    std::cout << MAX[counter].VacationLocation << '\n';
    std::cout << MAX[counter].SeatAlphabet << '\n';
}
Last edited on
If you really need to do it, you can do something silly like this:

1
2
3
4
5
6
char arr[30] = "LAKE GENEVA";

for(int i = 0; i < 30; i++)
MAX.VacationLocation[i] = arr[i];

std::cout << MAX.VacationLocation;

Output:
LAKE GENEVA
Last edited on
Yep. I also think its dumb.
Thank you very much. it helps a lot
you can use strcpy, you did something wrong. the loop works too, but you should revisit the strcpy as it is the right way to do it and looping every string in your class is going to bloat everything badly if you don't compact it.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cstring>
using namespace std; 

int main()
 {
   char result[100];
   strcpy(result, "happy place"); 
   cout << result << endl;      
 }
 
Last edited on
Topic archived. No new replies allowed.