0

kindly please help me with my problem

#include <stdio.h>
#include <string.h>
int samplepre ()
{
    do {

    char username[3]; // users's input username
    char password[3]; //user's input password
    char choice; // Main menu choice
    char x; // option try again
    char string;
    int i; // for for loop : index

    printf ("\t************\n");
    printf ("\t*Login Menu*\n");
    printf ("\t************\n");

    printf ("Enter your username:"); // The user is ask to input its username
    scanf  ("%s",username);
    printf ("Enter password:"); // The user is ask to input its password
    scanf  ("%s",password);

    if (strcmp(username,"admin") == 0 && strcmp(password,"vdedote") == 0)
    {  // if the condition is true the set of codes will execute
       system ("cls");
       printf ("****************\n");
       printf ("*WELCOME ADMIN!*\n");
       printf ("****************\n");
       system ("PAUSE");
       //printf ("\n");
       printf ("What may I be of service sir?\n");
       printf ("************\n");
       printf ("*MAIN MENU:*\n");
       printf ("************\n");
       //printf ("\n");          
       printf ("[A] Remove Vowels\n"); // The user will be asked of his own choice
       printf ("[B] Remove Entered Character\n");
       printf ("[C] Arithmetic\n");
       printf ("Select your choice:");
       scanf  ("%s",choice);

   }
    else 
    {     // if the user inputs wrong data
         // x = option try again
     printf ("****************\n");
     printf ("*ACCESS DENIED!*\n");
     printf ("****************\n");
     printf ("Would you like to try again <Y/N>");
     scanf  ("%s",x);
    }
    while (strcmp(username,"admin") == 1 && strcmp(password,"vdedote") == 1);
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
vdedote
  • 21
  • 5

5 Answers5

2
  1. You are missing a } before the while.
  2. You need to move username and password out of your loop.
  3. You shouldn't use scanf http://c-faq.com/stdio/scanfprobs.html
  4. You are not allocating enough space for your username and password

Maybe you should read a tutorial about proper reading first? The answer to this question looks pretty nice: how to read string entered by user in c

Community
  • 1
  • 1
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49
  • i want my program to do a do while loop , for example if the user entered the wrong username and password, i want the program to ask it all over again until it types the correct username and password. – vdedote Nov 06 '15 at 14:19
  • @vdedote Actually it is pretty clear what you are trying to do. You just made so many errors ... Your idea is correct in most part but the realization is lacking correctness... – Simon Kraemer Nov 06 '15 at 14:34
1

You're missing a closing brace } just before the while.

Once you've fixed this syntax error, be sure to understand that strcmp returns zero if the strings are equal.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • i want my program to do a do while loop , for example if the user entered the wrong username and password, i want the program to ask it all over again until it types the correct username and password. – vdedote Nov 06 '15 at 14:19
1

Because you are not closing the do !!

do{.......} 
while(...);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • i want my program to do a do while loop , for example if the user entered the wrong username and password, i want the program to ask it all over again until it types the correct username and password. – vdedote Nov 06 '15 at 14:19
1

thanks for your question,

  • After you corrected the syntactical errors (braces)
  • After you have made the indentation look better
  • After you have understood the idea of strcmp
  • After you have understood that doing it this way is VERY dangerous

then and only then:

should you end the do .. while .. loop with a conditional statement where it is checked IF the user is NOT the admin, then you repeat!

    while (strcmp(username,"admin") != 0 && strcmp(password,"vdedote") != 0);

so the loop knows it needs to repeat because it is not the admin with the right password login in!

hewi
  • 1,274
  • 3
  • 17
  • 32
0

The thing is strcmp() returns 0 when the strings are equal.

int strcmp(const char *s1, const char *s2);

RETURN VALUE

The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97