-2

this code is supposed to get username and password entered.. username is Admin and password is 2016.. if the user enters them correctly it'll print that the login process was done successfully else it'll ask the user to enter them again.. I made the code but it's not working and I don't know why.. here it's:

#include <stdio.h>
int main(){
    char* *username[5]; int password,choice; char Admin,i;

    printf("Welcome to the Students' Registration System\n");
    printf("Dear Registry employee: Kindly insert your username:\n");
    for (i=0;i<5;i++){
        scanf("%c", &username[i]);  
    }
    printf("Insert your password:\n");
    scanf("%d", &password);


    if ((*username[5]=="Admin")&&(password==2016))
    printf("The login process is successfully done");
    else 
    while ((*username[5]!="Admin")||(password!=2016))
   {
        printf("The login process failed\n");
        printf("Dear Registry employee: Kindly insert the correct username and password\n");
        for (i=0;i<5;i++){
            scanf("%c", &username[i]);
        }
        scanf("%d", &password); 
    }

    printf("Please choose the number of your next step:\n");
    printf("[1]Add new student\n");
    printf("[2]Add new course\n");
    printf("[3]Assign\remove courses for the student\n");
    printf("[4]Search and view students' details:\n");
    printf("[5]Request reports:\n"); 
    printf("[6]Update student/course record:\n");
    printf("[7]Delete student/course record:\n");

    return 0;
 }
SKD
  • 464
  • 1
  • 4
  • 16
Mariyah
  • 3
  • 5
  • When you say code is not working, what the problem you're facing? – niyasc May 10 '16 at 07:21
  • The compiler isn't showing any errors.. after running the program and entering the username and password the program stops running.. it gives a message that it stopped working.. – Mariyah May 10 '16 at 07:28
  • 4
    Multiple problems, many of which should give you warnings. For example, you can't compare string using the `==` or `!=` operators, as that will compare the *pointers* and not the strings they point to. – Some programmer dude May 10 '16 at 07:28
  • 3
    Your code has too many basic problems. I suggest you read a good book or tutorial – Spikatrix May 10 '16 at 07:28
  • 2
    Enable warnings if you do not get any. Use `-Wall -Wextra`. – Spikatrix May 10 '16 at 07:29
  • But I've to submit it tomorrow :( ... I'm still beginner.. is there any other way to do the same idea? – Mariyah May 10 '16 at 07:31
  • Do you know the `%s` format specifier for `scanf`? And how to define a simple character array? (Note that `5` is too short for a 5-letter password.) – Weather Vane May 10 '16 at 07:32

2 Answers2

2

There are multiple issues with your program, which includes:

  • Delcaration of username as array of pointers pointing to character pointers
  • Length of username is not sufficient enough to hold default password admin
  • Reading username using a loop.
  • Comparing string using == and != operator.

A better approach can be as follows.

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

int main()
{
    //Admin has 5 characters, and string requires one null terminator. So minimum length should be 6
    char username[10]; 
    int password,choice; 
    char Admin,i;

    printf("Welcome to the Students' Registration System\n");


    do
    {
        printf("Dear Registry employee: Kindly insert your username:\n");
        //Use %s to read a string completely(till white space character)
        scanf("%s", username);
        printf("Insert your password:\n");
        scanf("%d", &password);


        //You can't compare string using == or !=
    }while (strcmp(username, "admin") != 0 && password != 2016 );

    printf("The login process is successfully done");

    printf("Please choose the number of your next step:\n");
    printf("[1]Add new student\n");
    printf("[2]Add new course\n");
    printf("[3]Assign\remove courses for the student\n");
    printf("[4]Search and view students' details:\n");
    printf("[5]Request reports:\n"); 
    printf("[6]Update student/course record:\n");
    printf("[7]Delete student/course record:\n");

    return 0;
}
niyasc
  • 4,440
  • 1
  • 23
  • 50
  • `char * * username[5]` is an array of *pointers* to character pointers... also, using `scanf( "%s" )` on user input is a buffer overflow waiting to happen. – DevSolar May 10 '16 at 08:01
  • @DevSolar Yes. You're right. I missed one `*` in declaration. I have updated my answer. Also, there is a potential overflow as you points out. But I don't think it will matter in school homeworks. – niyasc May 10 '16 at 08:05
  • Thanks @niyasc :D I really understood and learnt some new stuff from you!! I gave my code to our teacher more than three weeks ago and she didn't figure the problem out yet! she's not cs student as it seems but they let her teach us anyway <\3 thanks a lot you don't know how much you made me happy :D :D :D – Mariyah May 10 '16 at 08:14
-1

How to read / parse input in C? The FAQ.. Start at the section "Do not use *scanf() for potentially malformed input", and read on from there.


I don't give ready-to-use answers to homework questions, but some hints on your particular problem:

char* *username[5]

This is an array of 5 pointers to pointers to char. Not what you want, really. You want an array of characters, a.k.a. a "string".

for (i=0;i<5;i++){
        scanf("%c", &username[i]);  
    }

This (%c) reads one character at a time. Again, you want a string. You could do that with scanf( "%s", ... ), but you really should not do that. You want fgets().

if ((*username[5]=="Admin")&&(password==2016))

username[5]? After you read one character at a time? You see the problem?

There is a function named strncmp that you might be interested in.

Community
  • 1
  • 1
DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • But why `fgets`? It is for files. Right? – niyasc May 10 '16 at 08:07
  • @niyasc: You can use all of the file-based functions on the predefined `FILE *` objects `stdin`, `stdout`, or `stderr` (`fgets( buffer, BUF_SIZE, stdin )`). This avoids various common issues with the `*scanf()` family: 1) buffer overflows, 2) confusion about whitespace skipping, 3) not checking the return code and operating on undefined / indeterminate values, 4) ending up at an unclear position in the input if more complex conversion specifiers fail (whereas the various `strto*()` functions are very explicit at which point they fail). It's generally good practice. – DevSolar May 10 '16 at 08:15
  • So you discourage abusing one function, and then without explaining how to safely use the other you encourage the other? You can do better with this answer... much better... – autistic Jul 17 '18 at 22:14
  • @DevSolar You don't teach people how to use `scanf` correctly, you don't teach people how to use `fgets` correctly... they're both dangerous in subtle ways... and yet you're happy to teach people how to use *one of them incorrectly*... but not the other... and you're *not* happy to fix mistakes, either... at least my criticism has mistakes for you to fix; yours is just "this would be a good answer if it didn't disagree with my advice"... crap! – autistic Jul 17 '18 at 22:35
  • [Your `fgets` manual](https://en.cppreference.com/w/c/io/fgets) is broken (and for C++, which is irrelevant), by the way... Probably best to use [the POSIX `fgets` manual](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html)... at least the example there presents itself as incomplete and explains what needs to be done. – autistic Jul 17 '18 at 22:38
  • @autistic: Right now you're harrassing me across half a dozen different comment threads. *This is not a discussion forum*. I will continue to suggest using `fgets()` instead of `scanf()` on user input, because that is the way I learned it myself, and -- in your case -- because I refuse to be bullied. This has been taken to meta. – DevSolar Jul 17 '18 at 22:38
  • @autistic: As I said in another comment thread you spread this whole issue across in the last 45 minutes, I find your tone inacceptable for constructive discourse. Condescending, at the very least. And while some of the points you bring up actually have merit (and have been corrected), you are not always right. It is not "my" `fgets()` manual, it's from cppreference.com, which is arguably the best online resource on both the C and C++ standard library apart from the standard documents themselves and used by many regular SO contributors. (t.b.c.) – DevSolar Jul 17 '18 at 22:52
  • (ctd.) And the link you gave *is* about C, not C++. [This](https://en.cppreference.com/w/cpp/io/c/fgets) is the corresponding C++ entry. If you found an error, you are free to contribute there as well. And now I wish you a good night. – DevSolar Jul 17 '18 at 22:55