Thread: If statement not working

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    33

    If statement not working

    The if statement inside the while loop is not working.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    void startup_message();
    
    int main ()
    {
        startup_message();
        char username[10];
        char correct_username[10];
        char second_part_of_correct_username[5];
        char firstname[10];
        char lastname[20];
        int attempts;
        long long int cin;
        long long int correct_password;
        long long int password;
    
        printf("Enter your first name:\n");
        scanf("%s",firstname);
    
        strncpy(correct_username,firstname,2);
        correct_username[2]='\0';
    
        printf("Enter your last name:\n");
        scanf("%s",lastname);
    
        strncpy(second_part_of_correct_username,lastname,3);
        second_part_of_correct_username[3]='\0';
        strncat(correct_username,second_part_of_correct_username,5);
        
        printf("Enter your Customer Identification Number(CIN):\n");
        scanf("%llu",&cin);
        correct_password=(cin%10)+1;
        printf("%d",correct_password);
        printf("Enter username:\n");
        scanf("%s",&username);
    
        printf("Enter password:\n");
        scanf("%llu",&password);
        
        if ((password==correct_password) && strcmp(username,correct_username)==0)
            {
                printf("Login successful!\n");
            }
        
        while ((password!=correct_password) || strcmp(username,correct_username)!=0)
            {
                printf("Login unsucessful! Please try again.\n");
                
                
                printf("Enter username:\n");
                scanf("%s",&username);
    
                printf("Enter password:\n");
                scanf("%llu",&password);
                ++attempts;
                if (attempts==5)
                    printf("Too many tries.\n");
                if (attempts==5)
                    break;
            }
        
        return 0;
    }
    
    void startup_message()
    {
        printf("Welcome to the newly introduced smsTransfer (a joint project of STARTRIGHT AND GLOBE TELECOM)!\n\n");
        printf("REMINDER: Both the transferers and the transferees must have active accounts with both companies to take part in this service.\n\n");
    }
    Last edited by howardbc14; 05-07-2015 at 05:52 PM.

  2. #2
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    First of all, when I compile your code I get several warnings.

    Code:
    $ gcc -Wall -pedantic -std=c99 -o osms osms.c
    osms.c: In function ‘main’:
    osms.c:33:5: warning: format ‘%llu’ expects argument of type ‘long long unsigned int *’, but argument 2 has type ‘long long int *’ [-Wformat]
    osms.c:35:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long long int’ [-Wformat]
    osms.c:37:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat]
    osms.c:40:5: warning: format ‘%llu’ expects argument of type ‘long long unsigned int *’, but argument 2 has type ‘long long int *’ [-Wformat]
    osms.c:53:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat]
    osms.c:56:13: warning: format ‘%llu’ expects argument of type ‘long long unsigned int *’, but argument 2 has type ‘long long int *’ [-Wformat]
    Try to resolve them first.

    Second, you don't give attempts an initial value so it will start with an random value;

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isalpha() If statement not working
    By BIGDENIRO in forum C Programming
    Replies: 13
    Last Post: 10-23-2013, 12:30 AM
  2. If statement not working
    By BIGDENIRO in forum C Programming
    Replies: 1
    Last Post: 10-10-2013, 02:58 PM
  3. The following if statement is not working!...
    By darkmagic in forum C Programming
    Replies: 5
    Last Post: 06-23-2010, 08:11 AM
  4. If/else statement not working
    By zenovy in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 08:26 PM
  5. If statement not working!
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2002, 02:45 AM