So, I made a program that lets the user type in his full name, showing its initials. (using a char array
, so the names will be delimited by a space).
This is my code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char name[100];
cout << "Name: "; cin >> name;
cout << name[0];
for (int i = 1; i<=strlen(name); i++)
if (name[i] == 32)
cout << name[i+1];
return 0;
}
It will find the space between the names and the things that will be shown are the first element from the array and the next element after the space.
For example, if I type in: John Doe
, the output will be: J D
.
My problem is, it only shows J
. Why?