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 <cstring>
using namespace std;
class User{
char (*username)[20] = new char[0][20];
char (*password)[20] = new char[0][20];
int *mobile = new int[0], size = 0, count = 0;
public:
User(){}
void addDetail(char name[20], char p[20], int m){
for(int i = 0; i < count; i++){
if(username[i] == name){
cout<<"\nUsername exists";
return;
}
}
size++;
username = (char (*)[20])realloc(username, size * sizeof(char[20]));
password = (char (*)[20])realloc(password, size * sizeof(char[20]));
mobile = (int*)realloc(mobile, size * sizeof(int));
strcpy(username[count], name);
strcpy(password[count], p);
mobile[count] = m;
count++;
return;
}
void update(char s[20]){
int i;
char temp[20];
for(i = 0; i < size; i++)
if(*s == *username[i]){
cout<<"Username found, enter password: ";
do{
cin>>temp;
if(*temp == *this->password[i]){
cout<<"Old number is "<<mobile[i]<<"\nEnter new number: ";
cin>>mobile[i];
cout<<"\nUpdate success!";
break;
}
else cout<<"Incorrect password! Try again: ";
}while(this->password[i] != temp);
}
}
void display(){
if(size == 0){
cout<<"\nNo details to display\n";
return;
}
for(int i = 0; i < size; i++){
cout<<"\nUsername: "<<this->username[i]<<endl;
cout<<"Mobile Number: "<<this->mobile[i]<<endl;
}
}
};
int main(){
int n, mobile,choice;
char username[20], password[20];
User users;
do{
cout<<"\nOptions\n";
cout<<"1. Add details\n";
cout<<"2. Update mobile number for the given name\n";
cout<<"3. Display\n";
cout<<"-1. Exit\n";
cin>>choice;
switch(choice){
case 1: cout<<"Input number of users: ";
cin>>n;
for(int i = 0; i < n; i++){
cout<<"User "<<i + 1<<endl;
cout<<"Username: ";
cin>>username;
cout<<"Password: ";
cin>>password;
cout<<"Mobile: ";
cin>>mobile;
users.addDetail(username, password, mobile);
} break;
case 2: cout<<"\nEnter name to change mobile number: ";
cin>>username;
users.update(username);
break;
case 3: users.display(); break;
case -1: break;
default: cout<<"\nInvalid choice"; break;
}
if(choice == -1) break;
}while(choice != -1);
return 0;
}
Comments
Leave a comment