c - loop not working properly, also needing data validation -
this may simple answer. have been programming c month. creating c-program user presented menu 1-4. menu options 1-3 ask user integer, when integer input, program writes or "draws" amount of "dots" or periods. each of first options same function, while loop, do-while loop, , loop respectively. way terminate program select 4 @ main menu.
my question how program loop , continue work properly. when execute program works correctly first time, when program loops main menu. no further options work. ie: if try input integer again "dot drawing" doesnt work correctly.
i having trouble validating input on either menu letters or "non numbers", @ moment if input letter breaks program.
im not sure , go this. im not needing code re-written, perhaps ideas of take it. i'll accept references or links provided. copy of incomplete program included below:
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { system("cls"); int programrun=0; int menuselection=0; int absmenuselection=0; int dotnumber=0; int countnumber=0; char enter; while(programrun==0) { system("cls"); printf("\nplease make number selection\n"); printf("please select choice:\n"); printf("[1] while loop...\n"); printf("[2] do-while loop...\n"); printf("[3] loop...\n"); printf("[4] exit program...\n\n"); scanf("%d%c",&menuselection,&enter); absmenuselection= abs(menuselection); if (absmenuselection <1 || absmenuselection>4) { } switch (absmenuselection) { case 1: printf("\nplease input number amount of dots wish see..."); scanf("%d", &dotnumber); if(dotnumber > 0) { while(countnumber<dotnumber) { countnumber++; printf("."); } } else{ printf("\nsorry, invalid response. have try again.\n"); } printf("\n"); system("pause"); break; case 2: printf("\nplease input number amount of dots wish see..."); scanf("%d", &dotnumber); if(dotnumber > 0) { { countnumber++; printf(".",countnumber); } while( countnumber<dotnumber ); } else { printf("\nsorry, invalid response. have try again.\n"); } printf("\n"); system("pause"); break; case 3: printf("\nplease input number amount of dots wish see..."); scanf("%d", &dotnumber); if(dotnumber > 0) { for(countnumber=0;countnumber<dotnumber;countnumber++) { printf(".",countnumber + 1); } } else { printf("\nsorry, invalid response. have try again.\n"); } printf("\n"); system("pause"); break; case 4: while(programrun>1) programrun=1; printf("\nokay have nice day"); return 0; default: printf("\nsorry invalid statement, try again\n\n"); system("pause"); }} system ("pause") ; return 0; }
its working .....
set after each case:
countnumber=0;
or try
if(dotnumber > 0) { countnumber=0; /*rest */ }
otherwise case 1: case 2: not work if try 2 times after giving dotnumber higher value @ first , lower @ last
edit: c library function void isdigit(int c) checks if passed character decimal digit character.
if( isdigit(variablehere) ) { //is digit }
sample o/p
please make number selection please select choice: [1] while loop... [2] do-while loop... [3] loop... [4] exit program... 1 please input number amount of dots wish see 4 .... press key continue . . .
Comments
Post a Comment