| Blantron (4) | |
|
//final review //==================================================== #include <iostream> #include <cstring> using namespace std; //===================================================== void displayInfo(char fullName[20], int & age); int main() { int age; char firstName[10] = "Allen"; char lastName[10]; char fullName[20]; cout << "Enter your age: " << endl; cin >> age; cout << "Enter your last name: " << endl; cin.getline(lastName, 10); cin.ignore(); strcpy(fullName, firstName); //<-----I'm pretty sure this is where my mistake is. strcpy(fullName, " "); // strcpy(fullName, lastName); displayInfo(fullName, age); return 0; } //==================================================== void displayInfo(char fullName[20], int & age) { cout << "Hello " << fullName[20] << ". "; cout << "You are " << age << " years old. " << endl; } /*output: Enter your age: 27 Enter your last name: McGowan Hello ╠. You are 27 years old. Press any key to continue . . .*/ | |
|
|
|
| Raezzor (223) | |||
I think what you want is to append not copy. I'm not sure, but if I remember correctly copy will overwrite any previous value. You could use simply concatenation I believe, like this:fullName = firstName + " " + lastName;The reason you are getting the funky output for name though is that you are using fullname[20] as your output. Since you fullName array is declared as having size 20, it has elements from 0 to 19. 20 is outside the array. Also, remember that the array is an array of characters, so trying to output a single element of the array will only display a single character. To output the entire array simply use fullName.Edit: Actually, <cstring> has a function called strcat that should work for you too. http://www.cplusplus.com/reference/cstring/strcat/ All you have to do is pass the name of the destination array, then the name of the string to be added. For you that call would look like:
| |||
|
Last edited on
|
|||
| Blantron (4) | |
| Thank you so much:) | |
|
Last edited on
|
|
| Disch (8336) | |||
|
Note that if you strcat, you must make sure the destination string (fullName) is initialized or you may overstep array bounds. I recommend first doing a strcpy, followed by strcats:
Of course this is much much easier and safer if you just use strings instead of char arrays... but this looks like a homework problem and I'm assuming you're not allowed to use strings. | |||
|
|
|||
| Blantron (4) | |
|
I used this code and it crashed after inputting the data, I changed what you told me to, maybe I put it in the wrong order:/ //final review //==================================================== #include <iostream> #include <string> #include <cstring> using namespace std; //===================================================== void displayInfo(char fullName[20], int & age); int main() { int age; char firstName[10] = "Allen"; char lastName[10]; char fullName[20]; cout << "Enter your age: " << endl; cin >> age; cout << "Enter your last name: " << endl; cin.getline(lastName, 10); cin.ignore(); strcat(fullName, firstName); strcat(fullName, " "); strcat(fullName, lastName); displayInfo(fullName, age); return 0; } //==================================================== void displayInfo(char fullName[20], int & age) { cout << "Hello " << fullName << ". "; cout << "You are " << age << " years old. " << endl; } When I used strcpy the program ran but it output nothing, and this was after changing fullName in the displayInfo function. | |
|
Last edited on
|
|
| Raezzor (223) | |
| Ah yup, Disch was right. Destination string should be initialized before you pass it to strcat. His method works well to using copy then cat. | |
|
Last edited on
|
|
| Blantron (4) | |
|
strcpy(fullName, firstName); strcat(fullName, " "); strcat(fullName, lastName); This only gives me the first name, I've tried several variations but I can't get the last name to output. The space outputs as well, but not the lastName array. | |
|
Last edited on
|
|