Thread: problem logging back in

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

    problem logging back in

    hi, im fairly new to programming, could any one help me with this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.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];
    char option_choice[20];
    int strcmp( const char *str1, const char *str2 );
    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("\n");
    	printf("MENU OPTIONS\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() {
    	int a = 3;
    	int b = 3;
    	printf("\n");
    	printf("WELCOME\n");
    	printf("\n");
    	while ((a, b) == 3 ) {
    		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("\n");
    			printf("YOURE 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("\n");
    				printf("USERNAME OR PASSWORD IS INCORRECT\n");
    				printf("YOU MAY NEED TO REGISTER\n");
    				printf("\n");
    			}
    			else {
    				while (strcmp (option_choice, "logout") != 0 ) {
    				        printf("\n");
    				        printf("WELCOME TO YOUR SYSTEM %s\n", user);
    					printf("\n");
    					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("\n");
    						printf("CURRENT USERNAME: %s\n", set_user);
    					 	printf("\n");
    						change_user(old_user, new_user, set_user);
    					}
    					else if (strcmp (option_choice, "pass") == 0 ) {
    						printf("\n");
    						printf("CURRENT PASSWORD: %s\n", set_pass);
    						printf("\n");
    						change_pass(old_pass, new_pass, set_pass);
    					}
    					else {
    					        printf("\n");
    						printf("INCORRECT INPUT\n");
    						printf("\n");
    					}
    				}
    				printf("\n");
    				printf("YOUVE NOW LOGGED OUT\n");
    				printf("\n");
    			}
    		}
    	}
    the problem is when i register and login its fine but when i logout then login again it logs out straight away because the option_choice variables value is always "logout", ive tried declaring option_ choice to a different value after the while option_choice is not equal to "logout" loop but it comes up with a error when i compile it.
    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    while ((a, b) == 3 )
    not what you think it should do.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    nonoob, i don't understand??

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's barely legal C... What are you trying to compare? Currently, a is ignored, and b is compared with 3.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    ive put a loop around the whole program with while a and b is equal to the number 3
    what do you mean by "barely legal C"?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You should write while (a ==3 && b == 3).
    You can not compare a set of values separated by commas. In C, the == is not commutative over comma separated statements.

    Anyway, I haven't checked the rest of your code. This one is just the obvious syntax problem that jumped out.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So what he's saying is that (a, b) == 3 is technically legal in C (i.e. it is grammatically correct), but not a construct anybody would use. Here is how it's evaluated:
    1. Look at the innermost parentheses. Evaluate statements (separated by commas) from left to right.
    2. The statement 'a' does nothing, but has a value of a.
    3. Then, the statement 'b' is evaluated. It also does nothing, but has a value of b.
    4. The parenthetical is given the value of the last statement, b:
    5. This value (b) is compared against 3.

    Notice that a is never compared to 3. What you need to do is separate these things out and compare them individually, then act accordingly if both a and b are 3:

    Code:
    while (a == 3 && b == 3)

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    kk, thanks i understand now, but can you help me with the option_choice variable please??

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by cprog12 View Post
    ive tried declaring option_ choice to a different value after the while option_choice is not equal to "logout" loop but it comes up with a error when i compile it.
    By declaring do you mean assigning it a different value? If so, how are you "assigning" it? I don't see that code in your post. It's worth noting that you can't simply assign strings in C the way you can an int, float, char. etc. You have to use strcpy. An alternative, that would simply make option_choice appear empty is to do option_choice[0] = '\0'. That makes the first character the null character, so there is no data in the string, just an "end of string" marker.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You are comparing option_choice before having initialized it.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    yes i mean assigning it, and ive tried this, i know it might look stupid but im new to C:
    Code:
    main() {
    	int a = 3;
    	int b = 3;
    	printf("\n");
    	printf("WELCOME\n");
    	printf("\n");
    	while (a == 3 && b ==3) {
    	        char option_choice = NULL;
    		print_first_options();
    		printf("Menu choice: ");
    		scanf("%s", menu_choice);
    		if (strcmp (menu_choice, "register") == 0 ) {
    Code:
    char option_choice = NULL;
    to try and change the value of the option_choice so that when i next login the value wont be "logout", which is causing it to logout straight away.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    ive initialized it as a global variable, should it be local???

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Not necessarily. Just intitialize it to null string right inside your main while loop.
    Code:
    option_choice[0] = '\0';

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    it works, thanks for that nonoob and the other guy

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Woohoo! And thanks anduril462 for clarifying comma separated statements. I use them so rarely ... only in for statements to do multiple initializations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  2. Problem with passing back pointer pointed to string
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-21-2007, 07:55 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Awkward problem in learning c++
    By TheUnknownFacto in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2007, 01:43 PM
  5. Passing Value back problem
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-10-2002, 05:22 PM