How to restart a loop after capturing a wrong input? because if i inputted a string or a character, my program captures the error but the problem is that my program does not return from the initial spot or initial position how can i solve that?this is my program Code: //This program can evaluate a 10th order polynomial single variable, //can solve for statistical algorithms mainly mean, standard deviation //and summation of data with a maximum of 10 data, //can solve for ideal gas equation with the gas sample of nitrogen and //can build a database of strings which can store of 16 strings with 128 //maximum characters, remove a string from the database, view the strings //and search the strings inside the database #include <stdio.h> #include <conio.h> #include <math.h> #include <stdlib.h> #include <string.h> //Jerome H. Macatangay 10821333 MEE 2nd Year //Andre Lloyd A. Torres 10832513 MEE 2nd Year //Start Program main(){ int selection; //Variables for Polynomial Evaluation double polysum=0, x; int n, QA, m; //Variables for Statistical Algorithms int SA,a,b,sum=0,data[10]; float mean; double calc,sd; //Variables for Ideal Gas Equations int unknown1; float P, V, R, M, T; R=8.3143/28.01; //Variables for String Database char string[16][128],temp[128],search[128]; int i=0,s=0,y,rep,sel,eq; //Menu Selection Screen do{ selection=0; system("cls"); printf("Menu Selection Screen\n\n"); printf("1 - Polynomial Evaluation\n"); printf("2 - Statistical Algorithms\n"); printf("3 - Ideal Gas Law\n"); printf("4 - String Database\n"); printf("5 - Exit\n"); scanf("%d",&selection); switch(selection){ case 1: do{ do{ system("cls"); printf("Polynomial Evaluation\n\n"); printf("Enter the Order of Polynomial (max. 10): "); scanf("%d",&n); if(n>10||n<1){ system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); } }while(n>10||n<1); double poly[n+1]; for(m=n;m>=0;m--){ printf("Please enter the constant for X raised to the %dth: ", m); scanf("%lf",&poly[m]); } printf("Enter the value of X: "); scanf("%lf",&x); for(m=n;m>=0;m--) polysum+=poly[m]*pow(x,m); printf("The evaluated value of the %dth Order Polynomial %.2lf\n", n, polysum); printf("Please press the spacebar to continue!!\n"); getche(); do{ system("cls"); printf("Do you want to repeat the computation?\n\n"); printf("1 - Yes\n"); printf("2 - No\n"); scanf("%d", &QA); switch(QA){ case 1: break; case 2: main(); break; default: system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); break; } }while(QA>2||QA<1); polysum=0; }while(QA==1); break; case 2: do{ sum=0; mean=0; calc=0; sd=0; system("cls"); printf("Statistical Algorithms\n\n"); printf("1 - Mean\n"); printf("2 - Standard Deviation\n"); printf("3 - Summation of data\n"); printf("4 - Go Back to Menu Selection Screen\n"); scanf("%d", &SA); switch(SA){ case 1: do{ system("cls"); printf("Mean\n\n"); printf("Enter the Number of Data (max. 10): "); scanf("%d", &b); if(b>10||b<1){ system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); } }while(b>10||b<1); for(a=0;a<b;a++){ printf("Enter the Actual Values of Data %d: ", a+1); scanf("%d",&data[a]); } for(a=0;a<b;a++){ sum=sum+data[a]; } mean=(float)sum/a; printf("The Mean of the Data is %f\n",mean); printf("Please press the spacebar to continue!!"); getche(); break; case 2: do{ system("cls"); printf("Standard Deviation\n\n"); printf("Enter the Number of Data (max. 10): "); scanf("%d", &b); if(b>10||b==0){ system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!\n"); getche(); } }while(b>10||b==0); for(a=0;a<b;a++){ printf("Enter the Actual Values of Data %d: ", a+1); scanf("%d",&data[a]); } if(b==1){ printf("The Standard Deviation the Data is 0\n"); printf("Please press the spacebar to continue!!"); getche(); break; } for(a=0;a<b;a++){ sum=sum+data[a]; } mean=(float)sum/a; for(a=0;a<b;a++){ calc+=pow((data[a]-mean),2); } sd=sqrt(calc/(b-1)); printf("The Standard Deviation the Data is %lf\n",sd); printf("Please press the spacebar to continue!!"); getche(); break; case 3: do{ system("cls"); printf("Summation of Data\n\n"); printf("Enter the Number of Data (max. 10): "); scanf("%d", &b); if(b>10||b<1){ system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); } }while(b>10||b<1); for(a=0;a<b;a++){ printf("Enter the Actual Values of Data %d: ", a+1); scanf("%d",&data[a]); } for(a=0;a<b;a++){ sum=sum+data[a]; } mean=(float)sum/a; printf("The Summation of the Data is %d\n",sum); printf("Please press the spacebar to continue!!"); getche(); break; case 4: main(); break; default: system("cls"); printf("Invalid Input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); break; } }while(SA!=4); break; case 3: do{ P=0; V=0; M=0; T=0; system("cls"); printf("Ideal Gas Law\n"); printf("PV = mRT\n\n"); printf("Select one of the Unknowns\n"); printf("1 - P\n"); printf("2 - V\n"); printf("3 - M\n"); printf("4 - T\n"); printf("5 - Go Back to Menu Selection Screen\n"); scanf("%d", &unknown1); switch(unknown1){ case 1: do{ system("cls"); printf("Solving for Pressure\n\n"); printf("Volume (m^3): "); scanf("%f",&V); printf("Mass (kg): "); scanf("%f",&M); printf("Temperature (K): "); scanf("%f",&T); if(V<=0){ system("cls"); printf("Invalid input of Volume\n"); printf("Please press the spacebar to continue!!"); getche(); } if(M<=0){ system("cls"); printf("Invalid input of Mass\n"); printf("Please press the spacebar to continue!!"); getche(); } if(T<=-273){ system("cls"); printf("Invalid input of Temperature\n"); printf("Please press the spacebar to continue!!"); getche(); } }while(V<=0||T<=-273||M<=0); P = M*R*T/V; printf("The Pressure is %f kPa\n",P); printf("Please press the spacebar to continue!!"); getche(); break; case 2: do{ system("cls"); printf("Solving for Volume\n\n"); printf("Pressure (KPa): "); scanf("%f",&P); printf("Mass (kg): "); scanf("%f",&M); printf("Temperature (K): "); scanf("%f",&T); if(P<=0){ system("cls"); printf("Invalid input of Pressure\n"); printf("Please press the spacebar to continue!!"); getche(); } if(M<=0){ system("cls"); printf("Invalid input of Mass\n"); printf("Please press the spacebar to continue!!"); getche(); } if(T<=-273){ system("cls"); printf("Invalid input of Temperature\n"); printf("Please press the spacebar to continue!!"); getche(); } }while(P<=0||T<=-273||M<=0); V = M*R*T/P; printf("The Volume is %f m^3\n",V); printf("Please press the spacebar to continue!!"); getche(); break; case 3: do{ system("cls"); printf("Solving for Mass\n\n"); printf("Pressure (KPa): "); scanf("%f",&P); printf("Volume (m^3): "); scanf("%f",&V); printf("Temperature (K): "); scanf("%f",&T); if(P<=0){ system("cls"); printf("Invalid input of Pressure\n"); printf("Please press the spacebar to continue!!"); getche(); } if(V<=0){ system("cls"); printf("Invalid input of Volume\n"); printf("Please press the spacebar to continue!!"); getche(); } if(T<=-273){ system("cls"); printf("Invalid input of Temperature\n"); printf("Please press the spacebar to continue!!"); getche(); } }while(P<=0||T<=-273||V<=0); M=(P*V)/(R*T); printf("The Mass is %f kg\n",M); printf("Please press the spacebar to continue!!"); getche(); break; case 4: do{ system("cls"); printf("Solving for Temperature\n\n"); printf("Pressure (KPa): "); scanf("%f",&P); printf("Volume (m^3): "); scanf("%f",&V); printf("Mass (kg): "); scanf("%f",&M); if(P<=0){ system("cls"); printf("Invalid input of Pressure\n"); printf("Please press the spacebar to continue!!"); getche(); } if(V<=0){ system("cls"); printf("Invalid input of Volume\n"); printf("Please press the spacebar to continue!!"); getche(); } if(M<=0){ system("cls"); printf("Invalid input of Mass\n"); printf("Please press the spacebar to continue!!"); getche(); } }while(P<=0||M<=0||V<=0); T = (P*V)/(M*R); printf("The Temperature is %f K\n",T); printf("Please press the spacebar to continue!!"); getche(); break; case 5: main(); break; default: system("cls"); printf("Invalid input, please try again!!\n"); printf("Please press the spacebar to continue!!"); getche(); break; } }while(unknown1 != 5); break; case 4: do{ do{//Menu Selection Screen do{ system("cls"); printf("String Database\n\n"); printf("1 - Store a String to the Database\n"); printf("2 - Remove a String from the Database\n"); printf("3 - View Strings in the Database\n"); printf("4 - Search a String in the Database\n"); printf("5 - Go Back to Menu Selection Screen\n"); scanf("%d",&selection); if(selection<1||selection>5){ system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); break; } }while(selection<1||selection>5); }while(selection!=1&&selection!=2&&selection!=3&&selection!=4&&selection!=5); if(selection==1){ do{//Store a String to the Database system("cls"); printf("How many string do you want enter: "); scanf("%d",&s); for(i=-1;i<s;i++){ printf("Input a string: "); gets(string[i]); } do{ do{ system("cls"); printf("String successfully added\n"); printf("Enter another string?\n\n"); printf("1 - Yes\n"); printf("2 - No\n"); scanf("%d",&rep); if(rep<1||rep>2){ system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); break; } }while(rep<1||rep>2); }while(rep!=1&&rep!=2); }while(rep==1); } else if(selection==2){ do{//Remove a String from the Database system("cls"); printf("Select position of string to remove:\n"); for(i=0;i<s;i++) printf("%d-%s\n",i+1,string[i]); scanf("%d",&y); strcpy(string[y-1],""); s--; strcpy(temp,string[s]); for(i=y;i<s;i++) strcpy(string[i-1],string[i]); strcpy(string[s-1],temp); do{ do{ system("cls"); printf("String successfully deleted\n"); printf("Remove another string?\n\n"); printf("1 - Yes\n"); printf("2 - No\n"); scanf("%d",&rep); if(rep<1||rep>2){ system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); break; } }while(rep<1||rep>2); }while(rep!=1&&rep!=2); }while(rep==1); } else if(selection==3){//View Strings in the Database system("cls"); for(i=0;i<s;i++) printf("%d - %s\n",i+1,string[i]); printf("Please press the spacebar to continue!!\n"); getch(); } else if(selection==4){ do{//Search a String in the Database system("cls"); eq=1; strcpy(search,""); for(i=-1;i<1;i++){ printf("Enter string to search: "); gets(search); } for(i=0;i<s;i++){ eq=strcmp(string[i],search); if(eq==0) break; } do{ system("cls"); if(eq==0){ printf("String matched\n"); printf("Position of searched string in array is %d\n",i+1); printf("Please press the spacebar to continue!!\n"); getche(); } else{ do{ printf("String not found\n"); printf("Search another string?\n\n"); printf("1 - Yes\n"); printf("2 - No\n"); scanf("%d",&rep); if(rep<1||rep>2){ system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); break; } }while(rep<1||rep>2); } }while(rep!=1&&rep!=2); }while(rep==1); } else if(sel==5){ main(); break; } }while(sel!=5); break; case 5: return 0; break; default: system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); break; } }while(selection>5 || selection<=0); }
if i understand correctly what you want try this Code: ................ char n1[10]; .................... do{ system("cls"); printf("Polynomial Evaluation\n\n"); printf("Enter the Order of Polynomial (max. 10): "); [COLOR=Red]scanf("%s",&n1); n=atoi(n1);[/COLOR] if(n>10||n<1){ system("cls"); printf("Invalid input. Please try again!!!\n"); printf("Please press the spacebar to continue!!"); getche(); } }while(n>10||n<1); ............................