sheela is working in a cyber. her current task is to create username and password for the users create a class user with the following attributes and initialize the constructor. write a c++ code to take the contact details of ‘n’ user and display it. add update() for those who want to change mobile number by giving their name. options 1. add details 2. update mobile number for the given name 3. display()
#include <iostream>
#include <string>
using namespace std;
class User{
private:
string username;
string password;
string mobileNumber;
public:
User(){}
User(string username,string password,string mobileNumber){
this->username=username;
this->password=password;
this->mobileNumber=mobileNumber;
}
string getUserName(){
return this->username;
}
string getPassword(){
return this->password;
}
string getMobileNumber(){
return this->mobileNumber;
}
void getUserName(string username){
this->username=username;
}
void getPassword(string password){
this->password=password;
}
void getMobileNumber(string mobileNumber){
this->mobileNumber=mobileNumber;
}
};
int getUserByUserName(User users[],int totaluUsers,string userName){
for(int i=0;i<totaluUsers;i++){
if(users[i].getUserName()==userName){
return i;
}
}
return -1;
}
int main(){
int ch=-1;
User users[1000];
int totaluUsers=0;
string username;
string password;
string mobileNumber;
while(ch!=4){
cout<<"1. Add details\n";
cout<<"2. Update mobile number\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";
cout<<"Your choice: ";
cin>>ch;
cin.ignore();
if(ch==1){
cout<<"Enter username: ";
getline(cin,username);
cout<<"Enter password: ";
getline(cin,password);
cout<<"Enter mobile number: ";
getline(cin,mobileNumber);
users[totaluUsers]=User(username,password,mobileNumber);
totaluUsers++;
}else if(ch==2){
if(totaluUsers>0){
cout<<"Enter username to update mobile number: ";
getline(cin,username);
int index=getUserByUserName(users,totaluUsers,username);
if(index!=-1){
cout<<"Enter a new mobile number: ";
getline(cin,mobileNumber);
users[index].getMobileNumber(mobileNumber);
}else{
cout<<"\nUsername does not exist.\n\n";
}
}else{
cout<<"\nUsers array is empty\n\n";
}
}else if(ch==3){
if(totaluUsers>0){
cout<<"Enter username to display: ";
getline(cin,username);
int index=getUserByUserName(users,totaluUsers,username);
if(index!=-1){
cout<<"\nEnter username: "<<users[index].getUserName()<<"\n";
cout<<"Enter password: "<<users[index].getPassword()<<"\n";
cout<<"Enter mobile numbe: "<<users[index].getMobileNumber()<<"\n\n";
}else{
cout<<"\nUsername does not exist.\n\n";
}
}else{
cout<<"\nUsers array is empty\n\n";
}
}else if(ch==4){
//exit
}else{
cout<<"\nWrong choice.\n";
}
}
return 0;
}
Comments
Leave a comment