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.

Respuesta :

Answer:

In C++:

#include <iostream>

#include <string>

#include <sstream>

using namespace std;

int main(){

   string lname, fname,stringnum;    int num; string login, pwd;

   cout<<"Last Name: ";    cin>>lname;

   cout<<"First Name: ";    cin>>fname;

   cout<<"Four-digit integer: ";    cin>>num;

   stringnum = to_string(num).substr(0,4);

   stringstream geek(stringnum);    geek>>num;

   num = num%100;

   pwd = to_string(num);

   if(lname.length()<5){ login = lname+fname.substr(0,1);    }

   else{ login = lname.substr(0,5)+fname.substr(0,1);    }

   cout<<"Login Name: "<<login<<endl;

   cout<<"Password: "<<pwd<<endl;

   return 0;

}

Explanation:

This declares all necessary variables

   string lname, fname,stringnum;    int num;     string login, pwd;

This prompts the user for last name

   cout<<"Last Name: ";    cin>>lname;

This prompts the user for first name

   cout<<"First Name: ";    cin>>fname;

This prompts the user for a four digit integer

   cout<<"Four-digit integer: ";    cin>>num;

This converts the input number to string and gets only the first four integer

   stringnum = to_string(num).substr(0,4);

This converts the string back to an integer

   stringstream geek(stringnum);    geek>>num;

This gets the last two of the four digit integer

   num = num%100;

This gets the password of the user

  pwd = to_string(num);

This gets the login name of the user.

This is executed if the length of the first name is less than 5

   if(lname.length()<5){ login = lname+fname.substr(0,1);    }

This is executed if otherwise

   else{ login = lname.substr(0,5)+fname.substr(0,1);    }

This prints the login name

   cout<<"Login Name: "<<login<<endl;

This prints the password

   cout<<"Password: "<<pwd<<endl;