229k views
17 votes
3.26 LAB: Login name 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. Ex: If the input is: Michael Jordan 1991 the output is: Your login name: JordaM91 Ex: If the input is: Kanye West 2024 the output is: Your login name: WestK24

1 Answer

7 votes

Answer:

In C++:

#include <iostream>

using namespace std;

int main(){

string fname,lname; int num;

cout<<"Firstname: "; cin>>fname;

cout<<"Lastname: "; cin>>lname;

cout<<"4 digits: "; cin>>num;

string login = lname;

if(lname.length()>=5){

login = lname.substr(0, 5); }

login+=fname.substr(0,1)+to_string(num%100);

cout<<login;

return 0;

}

Step-by-step explanation:

This declares the first name, last name as strings and 4 digit number as integer

string fname,lname; int num;

This prompts and get input for last name

cout<<"Firstname: "; cin>>fname;

This prompts and get input for first name

cout<<"Lastname: "; cin>>lname;

This prompts and get input for four digit number

cout<<"4 digits: "; cin>>num;

This initializes login information to last name

string login = lname;

This checks if length of login is 5 and above

if(lname.length()>=5){

If yes, it gets the first five letters only

login = lname.substr(0, 5); }

This adds the first character of first name and the last 2 digits of num to login

login+=fname.substr(0,1)+to_string(num%100);

This prints the login information

cout<<login;

User OysterD
by
7.3k points