When you use the name of the array by itself, e.g. "forename", that is automatically a pointer (the address of the first element of the array), so your code should work, except for one thing...
you're using a character array (which is not null terminated by default). You have no control over the names. If a user enters a name longer than the size of your array you're stuffed. Furthermore, you have no way of knowing until your program crashes.
using cin in this way results in undefined behaviour.
I don't advocate the style of programming (neither yours above above nor my compromise below, you should be using std::string), but at the very least, force the names to be a length you know
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
#include <cstdio>
using namespace std;
//Everything above this probably won't work with your old old compiler, so just ignore
int main()
{
// with a char array
char forename[10]; // for 9 characters and a terminating 0
scanf("%9s", forename); // will read at most 9 characters
// or with a string
string surname;
cin >> surname; // will read very long names
surname.resize(14); // restrict to 14 characters
// instead of strncpy and strcat
string username = forename[0] + surname;
cout << username << endl;
}
|
Lecture:
Judging from this code, you're using a very old compiler, even older than the 1998 standard. Given that people are already talking about C++14, I think you're being done / doing yourself a great disservice.
I appreciate that you may not be able to do much about the tools your lecturer gives, but you owe it to yourself to at least ask why this is the case, given that the newest and best compilers are available on multiple platforms, free of cost and open-source.
As you've taken the initiative in getting a book and going ahead of the class, i suggest that you go the whole way and and modernize in your own time, even if yo have to conform to old-timey methods in class.