How to run a C program that greeted only two users Alice and Bob. How to do it?

Iqra Ahmed 0 Reputation points
2023-03-26T11:14:20.88+00:00

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#
{count} votes

2 answers

Sort by: Most helpful
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-03-26T20:15:53.6666667+00:00
    
    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!");
                }
            }
        }
    }
    
    
    0 comments No comments

  2. 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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.