#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;
}
Please enter your first name:
Rachmainow
The first letter of your first name is: R
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';
}
Please enter your first name: Joe d'Ragman
The first letter of your name is: J
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!
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter your first name: ";
std::string firstName;
std::cin >> firstName;
std::cout << "Enter your middle initial: ";
std::string middleInitial;
std::cin >> middleInitial;
std::cout << "Enter your last name: ";
std::string lastName;
std::cin >> lastName;
std::cout << '\n';
std::cout << "Your full name is " << firstName << ' ' << middleInitial << ' ' << lastName << '\n';
}
Enter your first name: Joe D. Ragman
Enter your middle initial: Enter your last name:
Your full name is Joe D. Ragman
Enter your first name: Joe
Enter your middle initial: D.
Enter your last name: Ragman
Your full name is Joe D. Ragman
With std::string and std::getline:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter your full name: ";
std::string fullName;
std::getline(std::cin, fullName);
std::cout << '\n';
std::cout << "Your full name is: " << fullName << '\n';
}
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