-1

#include<stdio.h>

#include<string.h>

int main() {

 char username[5][10]={"akshay","shubham","gaurav","rahul","amit"};
 int i,a=1;
 char urname[10];
 char pass[10];

printf("enter the Username : ");     
scanf("%s",urname);

printf("enter the passwword : ");
 scanf("%s",pass);

     
        for(i=0;i<5;i++)
        {
            if(strcmp(&username[i][0],urname)==0)    //username check
            {
                if(strcmp("helloworld",pass)==0)      //password check
                {
                printf("correct username");
                break;
                }
                else 
                printf("wrong pass");
                break;
            }
        
            else
             printf(" wrong username");  
                           
    }   
        
           return 0;

    }  

//i wanted to make a login page but by some mean it is not working correctly please help me out...

  • Whats the input for `scanf()` ? Are your arrays big enough to hold it? – alex01011 Jun 26 '21 at 17:50
  • `pass` can't hold a string of more than 9 characters + \0. So it makes no sense to `strcmp()`... If you want to save `"helloworld"` into the `pass` buffer, increase its size, `pass[10]` -> `pass[11]` or something bigger, would be even better. – alex01011 Jun 26 '21 at 17:51
  • The size of `"helloworld"` is 11 and the string length is 10. To match that you need to overflow the input buffer. – Weather Vane Jun 26 '21 at 17:51
  • And you should never use `%s` format spec without restricting the input, here with `scanf("%9s", pass);` – Weather Vane Jun 26 '21 at 17:53
  • still getting an error – Onkar Chougule Jun 26 '21 at 17:54
  • 1
    Each time it compares the entered user name to a name in `username`, and the comparison fails, it is giving an error. That doesn't make sense. Instead, keep track of whether a match was found, and only give an unknown name error if *no* match was found. – Tom Karzes Jun 26 '21 at 17:55
  • can anybody help me to rewrite the code with please i will be very thankful for it... – Onkar Chougule Jun 26 '21 at 17:56
  • What kind of error are you getting? Works fine for me, https://godbolt.org/z/KTjbdYabK . – alex01011 Jun 26 '21 at 17:57
  • still get a wrong output .there is some logic problem in the program.it show print one time if the password or username does'nt found or wrong – Onkar Chougule Jun 26 '21 at 18:01
  • You need to move the position of the "wrong username" part to outside the loop, and use the `int a` that was defined as a flag. You should check *every* name before reporting an error. – Weather Vane Jun 26 '21 at 18:02
  • @OnkarChougule Then, include what you expected your program to output in your question... – alex01011 Jun 26 '21 at 18:02
  • @alex01011 please help me to rewrite the code because the logic is not working properly – Onkar Chougule Jun 26 '21 at 18:06
  • @WeatherVane can u help me by rewriting it so that i am to understand the problem in logic – Onkar Chougule Jun 26 '21 at 18:07
  • @OnkarChougule you are nearly there. See alex01011's version, and instead of reporting a mismatch before you checked them all, set `a = 0` when you found the match. Then use `a` to report after the loop has ended. – Weather Vane Jun 26 '21 at 18:10
  • @WeatherVane its not working giving an error of error: 'else' without a previous 'if' else – Onkar Chougule Jun 26 '21 at 18:11
  • The solution to that, is to take a break, make some coffee, and have another think about what you need to do. A 'pat' solution isn't going to help you! – Weather Vane Jun 26 '21 at 18:15

1 Answers1

0

Couple of things wrong with your code. First, the array size of 10 is insufficient for a string like "helloworld", in which we see 10 characters appear. You didn't count the '\0' byte at the end of the string. See this for details: What does the symbol \0 mean in a string-literal?

You also display an error immediately after you find a username mismatch. You should only do so in the end, after you have checked every entry in the username[][] array and perhaps set a flag.

#include <stdio.h>
#include <string.h>

int main(void)
{

    char username[5][10] = {"akshay", "shubham", "gaurav", "rahul", "amit"};
    int i, uname_flag = 0;
    char urname[11];
    char pass[11];

    printf("enter the Username : ");
    
    if (scanf("%10s", urname) != 1) // limit characters to be read to 10, to avoid buffer overflow
                                    // also check the return value of scanf for input failure
    {
        return 1;
    }

    printf("enter the passwword : ");

    if (scanf("%10s", pass) != 1)
    {
        return 1;
    }

    for (i = 0; i < 5; i++)
    {
        if (strcmp(username[i], urname) == 0) //username check
        {
            uname_flag = 1; // username is correct
            if (strcmp("helloworld", pass) == 0) //password check
            {
                printf("correct username & pass");
                break;
            }
            else
            {
                printf("wrong pass");
                break;
            }

        }
    }

    if (uname_flag == 0) // check outside the loop
    {
        printf("wrong username\n");
    }

    return 0;
}
alex01011
  • 1,670
  • 2
  • 5
  • 17