In: Computer Science
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the last name.
Hint: Use the to_string() function to convert numerical data to a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string login;
string first;
string last;
int number;
/* Type your code here */
return 0;
}
Source Code:
Output:
Code in text format (See above images of code for indentation):
#include<iostream>
#include<string.h>
using namespace std;
/*main function*/
int main()
{
/*variables*/
string login;
string first;
string last;
int number;
/*read first name from user*/
cout<<"Enter your first name: ";
getline(cin,first);
/*read last name from user*/
cout<<"Enter your last name: ";
getline(cin,last);
/*read 4 digit number from user*/
cout<<"Enter a 4-digit number: ";
cin>>number;
login="";
/*extract last two digits*/
number=number%100;
/*if length of last name below 5 then add total
name*/
if(last.length()<5)
login=login+last;
else
/*add first five character of last
name*/
login=login+last.substr(0,5);
/*add first character of first name*/
login=login+first[0];
/*add last two digits using to_string()*/
login=login+to_string(number);
/*print login name*/
cout<<"Your login name is: "<<login;
return 0;
}