#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
usingnamespace std;
int main()
{
int userName;
char firstLetter, lastLetter;
cout << "Please enter your first name: " << endl;
cin >> userName;
lastLetter = userName % 10;
firstLetter = userName - lastLetter;
if (firstLetter > 10){
firstLetter = firstLetter / 10;
cout << "The first letter of your first name is: " << firstLetter;
}
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string userName;
cout << "Please enter your first name: " << endl;
cin >> userName;
cout << "The first letter of your first name is: " << userName[0];
return 0;
}
Nothing fancy...no strings, no arrays, just a single char variable.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
int main()
{
char userName;
std::cout << "Please enter your first name: ";
std::cin >> userName;
std::cout << '\n';
std::cout << "The first letter of your name is: " << userName << '\n';
}
FurryGuy... Thank you so much! For some reason, I was INTENT on using a string to capture the information. Thank you for the clarification. This is very helpful and I will use it going forward.
MikeStgt... Thank you for explaining HOW to grab it from a string. This will be very helpful for future reference. You also pinpointed on how I was trying to grab it (using int), though I just needed to use a char and dump the rest of the data. Thanks!
Enter your full name: Joe D. Ragman
Your full name is: Joe D. Ragman
If you wanted to separate out the full name string into the individual parts you could use a std::stringstream and extract the full name into other strings as you do using std::cin, but that is something really FANCY.
https://en.cppreference.com/w/cpp/io/basic_stringstream