Thread: Menu didnt work as intended

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    3

    Menu didnt work as intended

    I have a program with 3 functions

    1. Counting consonants and vowel
    2.
    3.

    I'm using "switch case" and "do while" in this menu, the code is working but when I choose function 1, the menu loop and dont execute the fuction.
    I tried with function 2 and 3 but the menu still loop and dont do anything at all.

    Here is my code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    void func1();
    void func2();
    void func3();
    
    
    int main()
    {
            int choice;
    
    
            do
            {
            printf("\t\tMENU");
            printf("\n---------------------------------------\n");
            printf("\n1. Count consonants and vowels in a string.");
            printf("\n2. Login.");
            printf("\n3. Sorting string array alphabetically.");
            printf("\n4. Exit.");
            printf("\n\n-------------------------------------\n");
            printf("\nXin moi nhap vao lua chon cua ban: ");
            scanf("%d", &choice);
    
    
            switch(choice)
            {
                case 1: func1(); break;
                case 2: func2(); break;
                case 3: func3(); break;
                case 4: exit(0); break;
                default: printf("\nInvalid. Please choose again.\n");
            }
            } while(choice != 4);
        return 0;
    }
    
    
    void func1()
    {
        void kiemtrachuoi(char chuoi[100])
    {
        int consonant = 0, vowel = 0;
        for (int i = 0; chuoi[i] != '\0'; i++)
        {
            if(chuoi[i]== 'a' || chuoi[i]== 'i' || chuoi[i]== 'e' || chuoi[i]== 'u' || chuoi[i]== 'o' ||
               chuoi[i]== 'A' || chuoi[i]== 'I' || chuoi[i]== 'E' || chuoi[i]== 'U' || chuoi[i]== 'O')
            {
                vowel++;
            }
            else
            {
                consonant++;
            }
        }
        printf("\nString %s has %d vowels and %d consonants\n",chuoi,vowel,consonant);
        return;
    }
        char chuoi[100];
        printf("\nEnter a string: ");
        gets(chuoi);
        kiemtrachuoi(chuoi);
        return;
    }
    
    
    void func2()
    {
        void checklogin(char user[100], int pass)
    {
        char userSys[] = "admin";
        int passSys = 12345;
        if(strcmp(user, userSys) == 0 && pass == passSys)
        {
            printf("\nLogin successful!!!\n");
        }
        else
        {
            printf("\nLogin failed!!!\n");
        }
        return;
    }
        char user[100];
        int pass;
        printf("Enter username: ");
        gets(user);
        printf("\nEnter password: ");
        scanf("%d", &pass);
        checklogin(user,pass);
        return;
    }
    
    
    void func3()
    {
    void nhapTen(char ten[5][20])
    {
        for (int i = 0; i < 5; i++)
        {
            printf("\nEnter 5 names from keyboard %d: ",i+1);
            gets(ten[i]);
        }
    }
    
    
    void sapxepTen(char ten[5][20])
    {
        char temp[20];
        for (int i = 0; i < 4; i++)
        {
            for (int j = i + 1 ; j < 5; j++)
            {
                if (strcmp(ten[i], ten[j]) > 0)
                {
                    strcpy(temp, ten[i]);
                    strcpy(ten[i], ten[j]);
                    strcpy(ten[j], temp);
                }
            }
        }
    }
    
    
        char ten[5][20];
        nhapTen(ten);
        sapxepTen(ten);
        printf("Set of Strings in Ascending alphabetical order: \n");
        for (int i = 0; i < 5; i++)
        {
            printf("\nNumber %d is %s", i+1, ten[i]);
        }
        return;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Does that code even compile, it doens't compile for me:

    main.c||In function ‘func1’:|
    main.c|46|error: ISO C forbids nested functions [-Wpedantic]|
    main.c|66|error: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]|
    main.c||In function ‘func2’:|
    main.c|74|error: ISO C forbids nested functions [-Wpedantic]|
    main.c||In function ‘func3’:|
    main.c|101|error: ISO C forbids nested functions [-Wpedantic]|
    main.c|111|error: ISO C forbids nested functions [-Wpedantic]|
    main.c|140|error: unknown type name ‘Quick’|
    main.c|140|error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘to’|
    main.c|140|error: unknown type name ‘to’|

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    Does that code even compile, it doens't compile for me:
    Sir, I'm using Codeblock to compile and its working for me
    Here is the pic: Imgur: The magic of the Internet

    Here is the demonstration: Imgur: The magic of the Internet

    And it loops again: Imgur: The magic of the Internet
    Last edited by lebaosang1234; 10-17-2019 at 12:21 PM.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by lebaosang1234 View Post
    Sir, I'm using Codeblock to compile and its working for me
    Here is the pic: Imgur: The magic of the Internet

    Here is the demonstration: Imgur: The magic of the Internet

    And it loops again: Imgur: The magic of the Internet
    No, it's not! You don't have your warnings turned on and turned up to full!!!

    Code::Blocks is an IDE (editor), NOT a compiler! Code::Blocks calls the compiler, and other external applications. One of the Code::Blocks menus will have the options for error and warning levels. I don't use it so I can't advise you further.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you not using a project? Using Code::Blocks without a project is a very bad idea since you probably are not properly setting up the compiler options.

    I suggest you read the error/warning messages I posted and fix those issues.

    Using gets() is never a good idea since this function can never be used safely.

    Also modern C does not allow for the implementation of one function inside another function.

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    3
    Quote Originally Posted by rstanley View Post
    No, it's not! You don't have your warnings turned on and turned up to full!!!

    Code::Blocks is an IDE (editor), NOT a compiler! Code::Blocks calls the compiler, and other external applications. One of the Code::Blocks menus will have the options for error and warning levels. I don't use it so I can't advise you further.
    Quote Originally Posted by jimblumberg View Post
    Why are you not using a project? Using Code::Blocks without a project is a very bad idea since you probably are not properly setting up the compiler options.

    I suggest you read the error/warning messages I posted and fix those issues.

    Using gets() is never a good idea since this function can never be used safely.

    Also modern C does not allow for the implementation of one function inside another function.
    Thanks for your advices. I try to reinstall Codeblock and write the code from the beginning and the loop still happen.

    Then I read Jimblumberg's comment about gets() and I found out that gets() is the cause for the loop and the solution is to add getchar() after scanf(%d,choice) and it works perfectly.

    And I would like to thank Jimblumberg for your advices. I'll try avoid using gets() and nesting function.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Why do you feel the need to reinstall Code::Blocks? That will not correct your code!

    gets() has been removed from the C11 Standard. You should use fgets() instead. fgets() does bring in the whole line including the new line char. It is the programmer's responsibility to remove the new line from the end of the input string.

  8. #8
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    gets() has been removed from the C11 Standard

    You can take
    Code:
    scanf("%19[^\n]", ten[i]);
    This code is an example to tell 'scanf' for read all letter(including space) but not the Enter-key.
    The number 19 after the percent sign limits the number of character wher read into
    the string ten[i] to 19.

    Exampe code for this:
    Code:
    #include <stdio.h>
    #include <stdlib.h> //EXIT_SUCCESS
    
    void clpuf(void)
     {
     while (getc(stdin) != '\n')
        ;
     }
    
    int main()
    {
     int i;
     char c_posi[200],  c_nega[200];
    
     for (i = 0; i < 200; i++)
          c_posi[i] = c_nega[i] = 0;
    
     printf("Example positiv- and negativ list for scanf\n");
    
     printf("\nPlease enter a Sting like 123430abc  :  ");
    
    /** reads only characters where between the squere brackets
       * ends the input by the first Letter wher is not in the list  */
     scanf("%[0123456789+-*/=]", c_posi);
     printf("The string was: %s \n\n", c_posi);
    
     /** cleans keyboar buffer  */
     clpuf();
    
     printf("\nPlease enter a Sting like  asdfgh123  :  ");
     scanf("%[^1234567890]", c_nega);
     printf("Input was: %s \n\n", c_nega);
    
     clpuf(); /** clear keyboard buffer */
    
     printf("\nPlease enter a Sting with space-signs:  ");
     /** liest alle Zeichen ausser den Newline-Zeichen(Enter-Taste) ein*/
     ///scanf("%21[^\n]", c_nega); ---> wäre eine Begrenzung auf 21 Zeichen beim einlesen
     scanf("%[^\n]", c_nega);
     printf("Input was: %s \n\n", c_nega);
    
     printf("End of Program\n");
    return EXIT_SUCCESS;
    }
    Also i think it would be a better if you don*t define a function between an other.

    Ty this:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
     
     
    void kiemtrachuoi(char chuoi[100]); 
    void checklogin(char user[100], int pass);
    void sapxepTen(char ten[5][20]);
    void nhapTen(char ten[5][20]);
    
    void func1();
    void func2();
    void func3();
    void clpuf(void);
     
     
    int main(int argc, char **argv)
    {
            int choice;
     
     
            do
            {
            printf("\t\tMENU");
            printf("\n---------------------------------------\n");
            printf("\n1. Count consonants and vowels in a string.");
            printf("\n2. Login.");
            printf("\n3. Sorting string array alphabetically.");
            printf("\n4. Exit.");
            printf("\n\n-------------------------------------\n");
            printf("\nXin moi nhap vao lua chon cua ban: ");
            scanf("%d", &choice);
            clpuf();
     
            switch(choice)
            {
                case 1: func1(); break;
                case 2: func2(); break;
                case 3: func3(); break;
                case 4: exit(0); break;
                default: printf("\nInvalid. Please choose again.\n");
            }
            } while(choice != 4);
        return 0;
    }
     
    //  Zaehlt im String Paramteter 1 die Vokale und Konsonanten
    void kiemtrachuoi(char chuoi[100])
    {
        int i, consonant = 0, vowel = 0, wlen = 0;
        wlen = strlen(chuoi);
    
        for (i = 0; i < wlen; i++)
        {
            if(chuoi[i]== 'a' || chuoi[i]== 'i' || chuoi[i]== 'e' || chuoi[i]== 'u' || chuoi[i]== 'o' ||
               chuoi[i]== 'A' || chuoi[i]== 'I' || chuoi[i]== 'E' || chuoi[i]== 'U' || chuoi[i]== 'O')
            {
                vowel++;
            }
            else
            {
                consonant++;
            }
        }
        printf("\nString %s has %d vowels and %d consonants\n",chuoi,vowel,consonant);
        return;
    } 
    
    // Vergleicht Par1 mit internen String und Par2 mit interner Zahl
    void checklogin(char user[100], int pass)
    {
        char userSys[] = "admin";
        int passSys = 12345;
        if(strcmp(user, userSys) == 0 && pass == passSys)
        {
            printf("\nLogin successful!!!\n");
        }
        else
        {
            printf("\nLogin failed!!!\n");
        }
        return;
    } 
     
     
    void func1()
    {
        char chuoi[100];
        printf("\nEnter a string: ");
        scanf("%99[^\n]", chuoi);
        kiemtrachuoi(chuoi); //  Zaehlt im String Paramteter 1 die Vokale und Konsonanten
        return;
    }
     
    // Enter username -> Enter password -> checklogin(user,pass) -> return
    void func2()
    {
        char user[100];
        int pass;
        printf("Enter username: ");
        scanf("%99[^\n]", user);
        printf("\nEnter password: ");
        scanf("%d", &pass);
        checklogin(user,pass); // Vergleicht Par1 mit internen String und Par2 mit interner Zahl
        return;
    }
     
     
    void func3()
    {
        char ten[5][20];
        nhapTen(ten);    // Speichert 5 Worte in Array von Parameter 1 (ten)
        sapxepTen(ten);
        printf("Set of Strings in Ascending alphabetical order: \n");
        for (int i = 0; i < 5; i++)
        {
            printf("\nNumber %d is %s", i+1, ten[i]);
        }
        return;
    }
    
    void sapxepTen(char ten[5][20])
    {
     int i, j;
     char temp[20];
     
       for (i = 0; i < 4; i++)          // 0 bis 3
        {
         for (j = i + 1 ; j < 5; j++)   // 0 bis 4
          {
           if (strcmp(ten[i], ten[j]) > 0) // vergleicht 0-3 mit 0 bis 4 wenn par1 > par 2 strings tauschen
            {
             strcpy(temp,   ten[i]);
             strcpy(ten[i], ten[j]);
             strcpy(ten[j], temp);
            }
          }
        }
    }
    
    // Speichert 5 Worte in Array von Parameter 1
    void nhapTen(char ten[5][20])
    {
       for (int i = 0; i < 5; i++)
        {
         printf("\nEnter 5 names from keyboard %d: ",i+1);
         scanf("%19[^\n]", ten[i]);
         clpuf();
        }
    }
    
    void clpuf(void)
     {
     while (getc(stdin) != '\n')
        ;
     }

  9. #9
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Sorry guys, for the comments in german!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char* doesn't work as intended
    By Queue in forum C Programming
    Replies: 10
    Last Post: 09-10-2006, 05:05 AM
  2. how do you get a new menu to work?
    By andrewjf9 in forum Windows Programming
    Replies: 0
    Last Post: 11-19-2002, 02:43 PM
  3. my file created wont work when i insert a menu. why????
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-22-2002, 12:03 AM

Tags for this Thread