——————
——————
Last edited by キリアン; 03-31-2018 at 11:24 AM.
Hopefully this posts correctly. I'm looking to return to the main menu (see comments below). Thank you
Code:#include <stdio.h> #include <string.h> char pswd[20] = "password"; // Password char inpt[20] = "_"; // User Input char ysno[3] = "yes"; // Yes or No? (menu || exit) int choice; // Main Menu Selection int lenInpt; // User Input Length int lenPswd; // Password Length int result; // Result of strcmp(1,2) /* Looking for an option to return to main menu when a process is completed or the user input has an error */ // Changing Password int chngePswd(){ printf("\n\tEnter your new password\t(max 20 characters)\n\n\tPassword: "); scanf("%s", inpt); int lenInpt = strlen(inpt); if(lenInpt > 20){ printf("\tError! Your password must have less than 20 charachters\n\nLogging off…\n"); // ‹Return to MAIN MENU› return 1; } strcpy(pswd, inpt); printf("\nYour password has been updated\n"); // ‹Return to MAIN MENU› return 0; } // Displaying Password int dsplyPswd(){ printf("\n\tYour password is \"%s\"\n", pswd); int lenPswd = strlen(pswd); printf("\tThe length of your password is %d charachters\n\n", lenPswd); printf("Return to the main menu?\tEnter |yes| or |no|\n"); scanf("%s", inpt); int result = strcmp(ysno, inpt); if(result == 0){ // ‹Return to MAIN MENU› }else{ printf("Logging off…\n"); return 1; } return 0; } // Login & Main Menu int main(){ printf("Password: "); scanf("%s", inpt); int result = strcmp(pswd,inpt); if(result == 0){ printf("\n\tYou are now logged in\n\n「Main Menu」\n1\tChange Password\n2\tDisplay Password\n3\tLog Out\n\nSelection: "); scanf( "%d", &choice); switch(choice){ case 1: chngePswd(); break; case 2: dsplyPswd(); break; case 3: printf("Logging off…\n\n\tThank you for using this app!\n"); break; default: printf("Invalid Input!\n\nLogging out…\n"); // ‹Return to MAIN MENU› } } else{ printf("\n\tIncorrect Password!\n\n"); // ‹Return to MAIN MENU› return 1; } return 0; }
Last edited by キリアン; 03-31-2018 at 11:18 AM.
First of all, thisis not correct.char ysno[3] = "yes";
You forgot the space required for the 0-character at the end of yesno
It is also a good habit to NOT use global variables when you not realy need to., keep them local. You use the same variable names for local and global variables. Can be confusing and cause errors.
You can put the display of the main menu inside a while-loop which only stops if the user chooses option 3. Also then dsplyPswd() should return 3 and its value should be assigned to the variable choice.
Maybe something like this:
Code:int main() { int result = -1; int choice = 0; while(choice != 3) { if(result != 0) { printf("Password: "); scanf("%s", inpt); result = strcmp(pswd,inpt); if(result != 0) printf("\n\tIncorrect Password!\n\n"); } else { if(result == 0) { printf("\n\tYou are now logged in\n\n「Main Menu」\n1\tChange Password\n2\tDisplay Password\n3\tLog Out\n\nSelection: "); scanf( "%d", &choice); switch(choice) { .... ....
The code above is just a general idea, the rest you have to work out by yourself
Hi, sorry for the late reply. Thank you for your advice. I think the program is working now
Code:#include <stdio.h> #include <string.h> int main(){ char pswd[21] = "login"; char input[21]; int option = 0; int result; while(option != 3){ login: printf("Password: "); scanf("%s", input); result = strcmp(pswd,input); if(result == 0){ printf("\n\tYou are now logged in\n\n「Main Menu」\n"); start: printf("1\tChange Password\n2\tDisplay Password\n3\tLog Out\n\nSelection: "); scanf("%d", &option); int lenPswd = strlen(pswd); switch(option){ case 1: printf("\n\tEnter your new password\t(max 20 characters)\n\n\tPassword: "); scanf("%s", input); int lenInput = strlen(input); if(lenInput > 20){ printf("\tError! Password is too long\n\n"); goto start; } strcpy(pswd, input); printf("\nYour password has been updated\n\n"); goto start; case 2: printf("\nYour password is |%s|\nLength is %d characters\n\n", pswd, lenPswd); goto start; case 3: printf("Logging off…\n\n"); return 0; default: printf("Invalid Input!\n\n"); goto start; } } else if(result != 0){ printf("\n\tIncorrect Password!\n\n"); goto login; } } return 0; }
You should now try to do this with appropriate loops, not using goto's.
> if(lenInput > 20)
You never get to do this.
Sure, you can type in more than 21 characters, but your program is crippled from the moment the user enters the 21st character.
Ideally, use
char buff[BUFSIZ];
fgets(buff,BUFSIZ,stdin);
BUFSIZ is usually large (defined in stdio.h), and fgets() ensures you never have a buffer overflow.
Now use strlen() to determine if the input meets your other requirements.
But a word of warning, if you're going to use fgets(), beware that it doesn't play nice with scanf functions.
It's generally best to use fgets() for ALL input, then use sscanf, strlen, strcpy, strtod, ... to do the necessary validation and conversion on an in-memory string.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
The hard-coded password is visible from command line. (I named the executable pswd.)
You must truncate input to scanf to avoid buffer overflow.Code:$ strings pswd | head -n 20 | tail strcmp __libc_start_main __gmon_start__ GLIBC_2.7 GLIBC_2.4 GLIBC_2.2.5 UH-h login AWAVA AUATL $
Code:scanf("%20s", input);