Answer to Question #242317 in C++ for Astor

Question #242317

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


1
Expert's answer
2021-09-26T00:16:19-0400
#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;
 }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS