Creating a new user account.
★★★A gamertag is a persona used to identify players in online games. A gamertag can include letters, numbers and symbols, but no two players can share the same gamertag. This program allows a player to enter their preferred gamertag and prevent other players from claiming it.
Write a program that asks the user to enter a gamertag. If the gamertag already exists in a file an error message is output, otherwise the gamertag is appended to the file.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
check_exists that:players.txt for reading data.write_gamertag that:players.txt for appending data.get_gamertag that:check_exists function to determine if the gamertag is already taken.write_gamertag to record the gamertag.get_gamertag subprogram.players.txt file:
FluidYosta
IdolizedMero
TimotheosDark
DemonCamping
Enter your chosen gamertag:
DemonCamping
Sorry, that gamertag is already taken. Try again.
Enter your chosen gamertag:
DemonRushing
Welcome DemonRushing
Enter your chosen gamertag:
OnlyUseMePistol
Welcome OnlyUseMePistol
check_exists
// Check if the gamertag has already been taken
static bool check_exists(string gamertag)
{
---
bool exists = false;
string player;
---
// Trap file not found
try
{
---
StreamReader file = new StreamReader("players.txt");
---
// Check each gamertag in the file until found or eof
while ((player = file.ReadLine()) != null)
{
---
player = player.Trim();
// Gamertag found in file
if (player == gamertag)
{
---
exists = true;
---
}
---
}
---
file.Close();
---
}
---
catch (FileNotFoundException)
{
---
return false;
---
}
---
return exists;
}
write_gamertag
// Append the new gamertag to the file
static void write_gamertag(string gamertag)
{
---
StreamWriter file = new StreamWriter("players.txt", true);
file.WriteLine(gamertag.Trim());
file.Close();
}
get_gamertag
// Ask the user to choose a new gamertag
static void get_gamertag()
{
---
bool exists = true;
// Only allow gamertags that have not been taken
string gamertag = "";
---
while (exists)
{
---
Console.WriteLine("Enter your chosen gamertag:");
gamertag = Console.ReadLine().Trim();
exists = check_exists(gamertag);
---
// Tell the user the gamertag has been taken
if (exists)
{
---
Console.WriteLine("Sorry, that gamertag is already taken. Try again.");
---
}
---
}
---
write_gamertag(gamertag);
Console.WriteLine($"Welcome {gamertag}");
}