Search code examples
cscanfprogram-entry-point

Main Menu choice prompt is gathered even on sub Function scanf (C)


Im creating a program that has a Main Menu linked to a couple of other functions. This is the Main Menu Code

int main(){

 int iMenuChoice,exit;
 iMenuChoice=0;
 exit =1;

 while (exit !=0){
  system("cls");
  printf("**********Main Menu************\n");
  printf("*1) Cartesian Plane           *\n");
  printf("*2) Number to Words           *\n");
  printf("*3) b          *\n");
  printf("*4) c   *\n");
  printf("*5) d        *\n");
  printf("*6) e        *\n");
  printf("*7) Exit                      *\n");
  printf("*******************************\n");
  scanf("%d",&iMenuChoice);

  switch (iMenuChoice) {
            case 1:
                Cartisian();
                break;
            case 2:
                Num2Word();
                break;
            case 3:                 
                break;
            case 4:                 
                break;
            case 5:                 
                break;
            case 6:
                break;
            case 7:
                system("cls");
                printf("\n\nThank you for using our Program\n\n ");
                exit = 0;
                break;
            default:
                break;
        }
  }
  getche();
}

So I test my Number to words function which prompts for a User input (Int). But when I enter the number, The function will ask for another number (no printf prompt just a _ that waits for a number input), Lets call this mystery number X.

After the function is done converting the number, The Program will go to main menu for a split second and automatically goes to another function.

I realized that The mystery number X is somehow being used as an advance menu input! I dont know what i did wrong.

Here is the code for The num2Word function.

int Num2Word()
{

    int iWor,iOnes,iTens,iHundred,iThousand;
    system("cls");
    printf("Enter a number(max 3000)\n");
    scanf("%i \n",&iWor);

    if(iWor<=3001)
    {
    iOnes=iWor%10;
    iWor=iWor/10;
    iTens=iWor%10;
    iWor=iWor/10;
    iHundred=iWor%10;
    iWor=iWor/10;
    iThousand=iWor%10;

    Thousand(iThousand);
    Hundred(iHundred);
    if(iTens!=1)
    {
        Tenty(iTens);
        Ones(iOnes);
    }
    if(iTens==1)
    Exception(iOnes);
    }
    else{
    printf("beyond 3000");
    }
    getche();
    return 0;
}

The Tenty,Ones, Hundredth and Thousandths all use Switch, same structure as code below:

int Ones(int x)
{
    if(x!=0){
    switch(x)
    {
        case 1:
        printf("One");
        break;
        case 2:
        printf("Two");
        break;
        case 3:
        printf("Three");
        break;
        case 4:
        printf("Four ");
        break;
        case 5:
        printf("Five");
        break;
        case 6:
        printf("Six");
        break;
        case 7:
        printf("Seven");
        break;
        case 8:
        printf("Eight");
        break;
        case 9:
        printf("Nine");
        break;
        default:
        break;
    }
}
    return;
}

Since I cant post images yet ill try to show how the function output looks like


Enter a Number (Max 3000):
619

//I press enter here and nothing happens
3 //I must input another number for it to show the conversion, in this case number 3.


six hundred nineteen

After this, itll go back to main menu for a split sec and go straight to Main() Switch(iMenuChoice) Case 3.


Solution

  • There's no mystery number X or ghost :)

    Change

    scanf("%i \n",&iWor); 
    

    to

    scanf("%i",&iWor);
    

    The whitespace characters you have in the format string of scanf, ignores all whitespace characters. That's why you are forced to input a non-whitespace character.