Main Menu choice prompt is gathered even on sub Function scanf (C) -
im creating program has main menu linked couple of other functions. 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 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 using our program\n\n "); exit = 0; break; default: break; } } getche(); }
so test number words function prompts user input (int). when enter number, function ask number (no printf prompt _ waits number input), lets call mystery number x.
after function done converting number, program go main menu split second , automatically goes function.
i realized mystery number x somehow being used advance menu input! dont know did wrong.
here code num2word function.
int num2word() { int iwor,iones,itens,ihundred,ithousand; system("cls"); printf("enter 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 , thousandths use switch, same structure 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 cant post images yet ill try show how function output looks like
enter number (max 3000):
619
//i press enter here , nothing happens
3 //i must input number show conversion, in case number 3.
six hundred nineteen
after this, itll go main menu split sec , go straight main() switch(imenuchoice) case 3.
there's no mystery number x or ghost :)
change
scanf("%i \n",&iwor);
to
scanf("%i",&iwor);
the whitespace characters have in format string of scanf, ignores whitespace characters. that's why forced input non-whitespace character.
Comments
Post a Comment