using System;
namespace GreetUsersWithoutStrings
{
class Program
{
static int HashString(string s)
{
int hash = 0;
foreach (char c in s)
{
hash += c;
}
return hash;
}
static void Main(string[] args)
{
int aliceHash = HashString("Alice");
int bobHash = HashString("Bob");
Console.Write("Enter your name: ");
string inputName = Console.ReadLine();
int inputHash = HashString(inputName);
if (inputHash == aliceHash)
{
Console.WriteLine("Hello, Alice!");
}
else if (inputHash == bobHash)
{
Console.WriteLine("Hello, Bob!");
}
else
{
Console.WriteLine("Hello, stranger!");
}
}
}
}
How to run a C program that greeted only two users Alice and Bob. How to do it?
I want to figure out this program that greet only two specific user and this program should not contain any string.
Developer technologies | C++
Developer technologies | C#
2 answers
Sort by: Most helpful
-
Sedat SALMAN 14,180 Reputation points MVP
2023-03-26T20:15:53.6666667+00:00 -
YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
2023-03-27T03:01:47.2033333+00:00 Hi Iqra Ahmed,
I wrote a sample for your reference, it cannot be executed directly, you need to modify it according to the actual situation.
void login_user(void) { char name[20] = { 0 }; char passwd[7] = { 0 }; printf("enter user name:"); gets(name); if (0 == strcmp("Alice", name)) {//compare password printf("please input password:"); gets(passwd); if (0 == strcmp(Alice password, passwd)) { return; } } else if (0 == strcmp("Bob", name)) { printf("please input password:"); gets(passwd); if (0 == strcmp(Bob password, passwd)) { return; } } else { printf("you are not alicec or bob"); return; } }
This code mainly uses
strcmp
to determine whether the user is Alice or Bob, and it should be determined whether to execute the next step after the verification is passed.Best regards,
Elya
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.