Write a C++ program to get a list of usernames from the user, store it in a set and print the size of the set. Print " Invalid number " if the number of usernames to be added is less than or equal to zero.
In the main method, obtain input from the user in the console and insert the username into the set. Display the size of the set in the main method.
Problem constraints:
Use <set> container to store the values.
Use insert() method to insert the values.
Output format:
The output consists of an integer that corresponds to the size of the set.
If the number of usernames to be added is less than or equal to zero display "Invalid number ".
Output 1:
Enter the number of usernames to be added:
3
Enter the name of user 1
Enter the name:
arjun
Enter the name of user 2
Enter the name:
siva
Enter the name of user 3
Enter the name:
siva
Size of the set is:2
#include<iostream>
#include<set>
using namespace std;
int main()
{
int x,y;
string name_of_users;
set<string> users;
cout<<"Enter the number of usernames to be added: \n";
cin>>y;
if(y <= 0)
cout<<"Invalid number"<<endl;
else{
for(x = 1; x <= y; x++){
cout<<"Enter the name of user "<<x<<endl;
cout<<"Enter the name: \n";
cin>>name_of_users;
users.insert(name_of_users);
}
cout<<"Size of the set is: "<<users.size()<<endl;
}
return 0;
}
Comments
Leave a comment