I need help with this program

Why doesn't fullName[20] get the value I assign it(first + last name)???

-----------------------------------------------------------
#include<iostream>
#include<string>
using namespace std;

char firstName[10] = "Michael";
char lastName[10];
string fullName[20];
void displayInfo(int age, string fullName[20]);

int main()
{
int age;
cout << "Please enter your age: ";
cin >> age;
cin.ignore();
cout << "Please enter the last name: ";
cin.getline(lastName, 10);

fullName[20] = firstName[10] + " " + lastName[10] ;

displayInfo(age, fullName);

system("pause");
return 0;
}

void displayInfo(int age, string fullName[])
{
cout << "Hello " << fullName[20] << ". You are " << age << " years old.\n";
}
You're doing an illegal assignment for C++. You cannot simply just assign a char array to another char array.

You're gonna need to copy the first and last name arrays byte for byte into each other or use the actual std::string that you included at the top of the program.

Also you seem to be mixing datatypes of cstrings and std::strings. I don't know if this intentional, but because of this you seem to have made an array of 20 std::string members, and not a cstring of 20 characters.

Either:
1
2
3
4
5
6
7
8
#include <string>
// Some other code
std::string firstName = "Michael";
std::string lastName;
std::string fullName; // note that I do not define any array's here.

// More code
fullName = firstName + " " + lastName; // With std::strings 


or:
1
2
3
4
5
6
7
8
9
char firstName[10] = Michael;
char lastName[10];
char fullName[20];
//more code

for (int i = 0; i < 10; ++i) {
   fullName[i] = firstName[i];
   fullName[i+10] = lastName[i];
} // This can and may be buggy. 
Last edited on
firstName[10] is out of bounds of firstName, so the value there is undefined, same thing for lastName. Remember c++ is zero index, 0-9 are the ranges for those 2 variables.
I'm kinda confused because the teacher wants us to use c strings, and assign the values of those strings( an array with 10 characters for each first name and last name) into the variable fullName... so the output can be FIRST NAME + LAST NAME with space in between so I assumed it should be a string. (e.g MICHAEL JORDAN)..
Last edited on
i had to use concatenates, and only use c strings.. thanks guys for all your help..
Last edited on
Topic archived. No new replies allowed.