Here the switch() isn't working properly;not executing the function; & morever the while loop run twice when an invalid choice is given..

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define MAX 100

int vowel_count(char*);
int conso_count(char*);
void to_lower(char*);
void to_upper(char*);
void menu();

int main()
{
    int cnt=0;
    char data[MAX]=" ";
    bool flag=false;
    printf("Enter String:\n");
    gets_s(data);
    menu();
    while(flag==false)
    {
        char ch;
        printf("\nEnter ur choice:\t");
        scanf("%c",&ch);
        switch (toupper(ch))
        {
        case 'A':
            cnt=vowel_count(data);
            printf("Vowels: %d",cnt);
            break;
        case 'B':
            cnt=conso_count(data);
            printf("Consonants: %d",cnt);
            break;
        case 'C':
            to_upper(data);
            break;
        case 'D':
            to_lower(data);
            break;
        case 'E':
            printf("\n%s\n",data);
            break;
        case 'F':
            printf("\nEnter new String:\n");
            gets_s(data);
            break;
        case 'M':
            menu();
            break;
        case 'X':
            flag=true;
            break;
        default:
            printf("Check Again");
            break;
        }
    }
    return 0;
}

void menu()
{
    printf("\nA)  Count the number of vowels in the string\n");
    printf("B)  Count the number of consonants in the string\n");
    printf("C)  Convert the string to uppercase\n");
    printf("D)  Convert the string to lowercase\n");
    printf("E)  Display the current string\n");
    printf("F)  Enter another string\n");
    printf("\n\nM)  Display this menu\n");
    printf("X)  Exit the program\n"); 
}

The problem most likely is that after you enter the Choice you then press the Enter key. After line 25 the Enter key '\n' is still in the keyboard buffer because scanf() didn't remove it so the next time scanf() is called it appears to do nothing because it sees the '\n' enter key. To fix that problem you need to remove the '\n' from the keyboard buffer. There are several ways to do it but the easiest is probably

char ch[2];
scanf("%1s",ch);
switch(toupper(ch[0])
{


}

thank you... the repetation of statement asking for menu operation is solved but still whenever a selection is entered, though it is a valid input it goes to default, instead of perfrming the task.

Insert a new line between lines 25 and 26 to print out the value of ch so that you can see what's happening.

You may like to see this version using fixedFgets ...

to see another way to handle input problems in C.

/* demo 'fixedFgets' and 'keeping flushed' the stdin stream... */

#include <stdio.h>
/* #include <stdlib.h> */
#include <string.h>
#include <ctype.h>

#define MAX 1024
#define FALSE 0
#define TRUE  1

int vowel_count( const char* );
int conso_count( const char* );
void to_all_lower( char* );
void to_all_upper( char* );


/* example of a way to fix fgets TWO very annoying problems   */
char* fixedFgets( char* s, size_t bufSize, FILE* fin )
{
    if( fgets( s, bufSize, fin ) )
    {
        char *p = strchr( s, '\n' ),
             c ;
        if( p ) *p = 0; /* strip off '\n' at end ... iF it exists */
        else while( (c = fgetc( fin )) != '\n'  &&  c != EOF ) ; /* flush... */
        return s;
    }
    /*  else ... if reach here ... */
    return NULL;
}

int showMenu_getChoice()
{
    int choice;
    printf( "A)  Count the vowels in the string\n" );
    printf( "B)  Count the consonants in the string\n" );
    printf( "C)  Convert to all uppercase\n" );
    printf( "D)  Convert to all lowercase\n" );
    printf( "E)  Display the current string\n" );
    printf( "F)  Enter a new string\n\n" );

    printf( "X)  Exit the program\n" );

    printf( "\nEnter your choice: " );

    choice = toupper( getchar() );
    while( getchar() != '\n' ) ; /* 'flush' stdin as you go ... */

    return choice;
}



int main()
{
    char data[MAX];
    int flag = FALSE;

    printf( "Enter String: " );
    fixedFgets( data, MAX, stdin );

    while( !flag )
    {
        switch ( showMenu_getChoice() )
        {
        case 'A':
            printf( "\nVowels: %d\n\n", vowel_count(data) ) ;
            break;
        case 'B':
            printf( "\nConsonants: %d\n\n", conso_count(data) );
            break;
        case 'C': to_all_upper(data); break;
        case 'D': to_all_lower(data); break;
        case 'E':
            printf( "\nThe string is: %s\n\n", data );
            break;
        case 'F':
            printf( "\nEnter new String: " );
            fixedFgets( data, MAX, stdin );
            break;

        case 'X': flag = TRUE;  break;
        default : printf( "\nNot implemented here ... try again.\n\n" );
        }
    }

    return 0;
}



int vowel_count( const char* s )
{
    int v_cnt = 0;
    while( *s != 0 )
    {
        if( strchr( "AEIOUaeiou", *s ) )
            ++v_cnt;
        ++s;
    }  
    return v_cnt;
}
int conso_count( const char* s )
{
    int c_cnt = 0;
    while( *s != 0 )
    {
        if( isalpha(*s) && !strchr( "AEIOUaeiou", *s ) )
            ++c_cnt;
        ++s;
    }
    return c_cnt;
}

void to_all_lower( char* s )
{
    while( *s ) ++s; /* to be completed */
}
void to_all_upper( char* s )
{
    while( *s )  ++s; /* to be completed */
}

try using cin.clear() in between lines 25 and 26

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.