Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;


void displayRules()
{
	cout<<"    Username Generator    "<<endl;
	cout<<"--------------------------"<<endl;
	cout<<"-       How to use:      -"<<endl;
	cout<<"-      Enter 3 Names     -"<<endl;
	cout<<"--------------------------"<<endl;
	cout<<" "<<endl;
}

int main() 
{
{
	string FName, SName, TName;
	
	displayRules();
	
	cout<<"Enter First Name: "<<endl;
	cin >> FName;
	
	cout<<"Enter Second Name: "<<endl;
	cin >> SName;
	
	cout<<"Enter Third Name: "<<endl;
	cin >> TName;
	
	string n = FName + SName + TName;
	cout<<n += n[n%2][rand()%n[n%2]];
	n[0] = toupper(n[0]); 
}


	return 0;
}


What I have tried:

, i ask a user to input 3 names then output 2 of them randomly. EX: input 1st name: christian 2nd name: joson third name: code then the output is christian code (i dont know if im correct that i used rand function but i dont know how to work with it with strings)
Posted
Updated 1-May-22 20:25pm

1 solution

That would be simple if you assign the names to an indexed container, like for instance a std::array<string,3> or a std::vector<string>. Then you just need to pick randomly two different indexes.

[update]
Quote:
I dont know what you mean. I'm so sorry I'm new to c++ and the vector or std is a new to me.
Then, it's time to learn. Try, for instance:
C++
#include <iostream>
#include <array>
using namespace std;
int main()
{
  // ITEM no          0      1      2 
  array<string,3> a{"foo", "boo", "goo"}; // array of 3 strings
  cout << a[1] << "\n"; // here you are using item '1' of a, that is "boo"
}

picking randomly an integer, say i, in the [0..2] range, your random picked string would be a[i].
[/update]
 
Share this answer
 
v2
Comments
Otaku Sensei 2-May-22 3:02am    
I dont know what you mean. I'm so sorry I'm new to c++ and the vector or std is a new to me.
CPallini 2-May-22 3:12am    
See my updated solution.
Otaku Sensei 2-May-22 4:11am    
i know how array works but still I don't know the random so I did some tweaking around the net and at the end I used switch case and array I think. (here's my code, well if u guys can suggest how to make a better code and easier I highly appreciate it.)

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
using namespace std;


void displayRules()
{
cout<<" Username Generator "<<endl;
cout<<"--------------------------"<<endl;
="" cout<<"-="" how="" to="" use:="" -"<<endl;
="" enter="" 3="" names="" cout<<"="" "<<endl;
}

int="" main()=""
{
="" char="" ans;
="" do
=""
="" {
="" string="" a,="" b,="" c;="" a.="" first="" name,="" b.="" second="" c.="" third="" name
="" int="" i;
="" displayrules();
="" cout<<"enter="" name:="" "<<endl;
="" cin="">> a;

cout<<"Enter Second Name: "<<endl;
cin="">> b;

cout<<"Enter Third Name: "<<endl;
cin="">> c;

cout<<"1. "<< a + " "<< b<<endl;
cout<<"2.="" "<<="" a="" +="" "="" c<<endl;
="" cout<<"3.="" b="" a<<endl;
="" cout<<"4.="" cout<<"5.="" c="" cout<<"6.="" b<<endl;
="" cin="">> i;

switch (i)
{
case 1:
{
cout <<"Initials for your Username is: "<<a[0]<<"_"<<b[0] <<="" "\n";
="" break;
="" }
=""
="" case="" 2:
="" {
="" cout="" <<"initials="" for="" your="" username="" is:="" "<<a[0]<<"_"<<c[0]="" 3:
="" "<<b[0]<<"_"<<a[0]="" 4:
="" "<<b[0]<<"_"<<c[0]="" 5:
="" "<<c[0]<<"_"<<a[0]="" 6:
="" "<<c[0]<<"_"<<b[0]="" }
}
="" "would="" you="" like="" to="" run="" the="" program="" again?="" (y="" n)="" ";
="" cin="">>ans;
}
while (ans == 'y' or ans == 'Y');
return 0;
}
CPallini 2-May-22 4:57am    
random is:
i=rand()%3;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900