help with loop

This is a discussion on help with loop within the C Programming forums, part of the General Programming Boards category; Hi, im having trouble with this program: Code: #include <stdio.h> #include <string.h> char set_user[20]; char set_pass[20]; char user[20]; char pass[20]; ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    45

    help with loop

    Hi, im having trouble with this program:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char set_user[20];
    char set_pass[20];
    char user[20];
    char pass[20];
    char old_pass[20];
    char new_pass[20];
    char old_user[20];
    char new_user[20];
    char menu_choice[20];
    int a = 3;
    int b = 3;
    int strcmp( const char *str1, const char *str2 );
    char option_choice[20];
    void print_options() {
    	printf("________________________________________\n");
    	printf("TYPE 'user' TO CHANGE YOUR USERNAME\n");
    	printf("TYPE 'pass' TO CHANGE YOUR PASSWORD\n");
    	printf("TYPE 'logout' TO LOGOUT\n");
    	printf("TYPE 'options' TO PRINT OPTIONS\n");
    	printf("________________________________________\n");
    }
    void change_pass(char *str1, char *str2, char *str3) {
    	printf("Old Password: ");
    	scanf("%s", str1);
    	printf("New Password: ");
    	scanf("%s", str2);
    	if (strcmp (str1, str3) == 0 ) {
    		str3 = str2;
    		printf("YOUR PASSWORD HAS NOW CHANGED\n");
    	}
    	else {
    		printf("INCORRECT OLD PASSWORD\n");
    	}
    }
    void change_user(char *str1, char *str2, char *str3) {
    	printf("Old Username: ");
    	scanf("%s", str1);
    	printf("New Username: ");
    	scanf("%s", str2);
    	if (strcmp (str1, str3) == 0 ) {
    		str3 = str2;
    		printf("YOUR USERNAME HAS NOW CHANGED FROM %s TO %s\n", str1, str2);
    	}
    	else {
    		printf("INCORRECT OLD USERNAME\n");
    	}
    }
    void print_first_options() {
    	printf("WELCOME\n");
    	printf("_______________________________\n");
    	printf("TYPE 'register' TO REGISTER\n");
    	printf("TYPE 'login' TO LOGIN\n");
    	printf("TYPE 'users' TO SHOW USERS\n");
    	printf("TYPE 'options' TO PRINT OPTIONS\n");
    	printf("_______________________________\n");
    }	
    
    main() {
    	while(strcmp (a, b) == 0 ) {
    		
    		print_first_options();
    		printf("Menu choice: ");
    		scanf("%s", menu_choice);
    		if (strcmp (menu_choice, "register") == 0 ) {
    			printf("REGISTER\n");
    			printf("SET YOUR USERNAME\n");
    			printf("Username: ");
    			scanf("%s", set_user);
    			printf("SET YOUR PASSWORD\n");
    			printf("Password: ");
    			scanf("%s", set_pass);
    			printf("Your now Registered\n");
    		}
    		else if (strcmp (menu_choice, "login") == 0 ) {
    			printf("Username: ");
    			scanf("%s", user);
    			printf("Password: ");
    			scanf("%s", pass);
    			if (strcmp (user, set_user) != 0 || strcmp (pass, set_pass) != 0 ) {
    				printf("USERNAME OR PASSWORD IS INCORRECT\n");
    				printf("YOU MAY NEED TO REGISTER\n");
    			}
    			else {
    				printf("WELCOME TO YOUR SYSTEM %s\n", user);
    				print_options();
    				printf("Option choice: ");
    				scanf("%s", option_choice);
    				if (strcmp (option_choice, "options") == 0 ) {
    					print_options();
    				}
    				else if (strcmp (option_choice, "user") == 0 ) {
    					printf("CURRENT USERNAME: %s\n", user);
    					change_user(old_user, new_user, user);
    				}
    			}
    		}
    	}
    }
    when i compile and run:
    Code:
    /root/Program Files/welcome_to_your_system.c: In function 'main':
    /root/Program Files/welcome_to_your_system.c:62: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast
    /root/Program Files/welcome_to_your_system.c:62: warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast
    can someone please tell me what im doing wrong

    Thanks.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    How about you point out which line is line 62, so we don't have to count them?

    And your reliance on global variables is very bad.

    EDIT: Oh, it's this one
    Code:
    while(strcmp (a, b) == 0 ) {
    What datatypes are your a and b variables? Are they char*s?
    Last edited by rags_to_riches; 12-07-2010 at 10:21 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    strcmp only compares strings. Did you look at line 62? Did you see what you put in there? Actually, I'm not sure why you put that there, since a and b never change....

    (Also: For the love of all that is good, get that prototype for strcmp out of there. You're including the right header file, let it do its job.)

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    	if (strcmp (str1, str3) == 0 ) {
    		str3 = str2;
    I'm sorry to say you cannot assign strings like that. You will need to use strcpy()

    What you get doing it that way is both pointers pointing to the same address, but the contents of the strings themselves are not copied.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 05:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 10:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2009, 12:54 AM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21