Hello
I made the following program with C language - but I do not know how to fix the error when the user enters characters instead of numbers;
For Example:
the values entered by the user must be numbers only not characters - and if he type character the program should give him error message says that " please enter numbers only"
how to do this.
appreciate any help ?
#include <stdio.h>
#include <conio.h>
float fs;
float t;
float f;
float q;
float c;
int x,i,a;
int S,M,H;
int main()
{
char Ans;
for (i=1;i<=5;i=i+a)
{
i--;
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" >> Welcome To Audio System >> \n");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" Please Enter: \n");
printf(" \n");
printf(" [1] To calculate File Size \n");
printf(" \n");
printf(" [2] To calculate Time \n");
printf(" \n");
printf(" [3] To calculate Frequnecy \n");
printf(" \n");
printf(" [4] To calculate Quantization \n");
printf(" \n");
printf(" [5] To calculate Channels \n");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
scanf("%d",&x);
if (x==1)
{
printf(" Please Enter Time \n Seconds\n ");
scanf("%d",&S);
printf(" \n");
printf(" Minutes\n ");
scanf("%d",&M);
printf(" \n");
printf(" Hours\n ");
scanf("%d",&H);
printf(" \n");
t=S+(M*60)+(H*60*60);
printf(" Please Enter Frequnecy \n ");
scanf("%f",&f);
printf(" \n");
printf(" Please Enter Quantization \n ");
scanf("%f",&q);
printf(" \n");
printf(" Please Enter Channels \n ");
scanf("%f",&c);
printf(" \n");
fs=t*(f*1000)*q*c;
fs=fs/8/1024/1024;
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The file size is %f MB \n",fs);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else if (x==2)
{ printf(" Please Enter \n File Size \n ");
scanf("%f",&fs);
printf(" Please Enter Frequnecy \n ");
scanf("%f",&f);
printf(" Please Enter Quantization \n ");
scanf("%f",&q);
printf(" Please Enter Channels \n ");
scanf("%f",&c);
t= fs/(f*q*c);
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The time is %f s \n",t);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else if (x==3)
{ printf(" Please Enter File Size \n ");
scanf("%f",&fs);
printf(" Please Enter Time \n Seconds\n ");
scanf("%d",&S);
printf(" Minutes\n ");
scanf("%d",&M);
printf(" Hours\n ");
scanf("%d",&H);
t=S+(M*60)+(H*60*60);
printf(" Please Enter Quantization \n ");
scanf("%f",&q);
printf(" Please Enter Channels \n ");
scanf("%f",&c);
f= (fs/(t*q*c))/1000;
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The Frequnecy is %f KHz \n",f);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else if (x==4)
{ printf(" Please Enter \n File Size \n ");
scanf("%f",&fs);
printf(" Please Enter Time \n Seconds\n ");
scanf("%d",&S);
printf(" Minutes\n ");
scanf("%d",&M);
printf(" Hours\n ");
scanf("%d",&H);
t=S+(M*60)+(H*60*60);
printf(" Please Enter Frequnecy \n ");
scanf("%f",&f);
printf(" Please Enter Channels \n ");
scanf("%f",&c);
q= fs/(t*f*c);
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The Quantization is %f \n",q);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else if (x==5)
{
printf(" Please Enter \n File Size \n ");
scanf("%f",&fs);
printf(" Please Enter Time \n Seconds\n ");
scanf("%d",&S);
printf(" Minutes\n ");
scanf("%d",&M);
printf(" Hours\n ");
scanf("%d",&H);
t=S+(M*60)+(H*60*60);
printf(" Please Enter Frequnecy \n ");
scanf("%f",&f);
printf(" Please Enter Quantization \n ");
scanf("%f",&q);
c= fs/(t*f*q);
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The Channels is %f \n",c);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else
{
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf("\t\t\t\t \n Sorry you have typed wrong input\n \n ");
printf("\t\t\t\t \n Please Enter Correct Number from [1] TO [5] \n\n ");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
do
{
printf ("\t\t Please TYPE 'y' To Exit To The Main Menu ");
scanf ("%c", &Ans);
if(Ans == 'n' || Ans =='N')
printf("\t\t \n Could you Please Leave Me Now...\n\n", Ans);
else if(Ans == 'y' || Ans == 'Y')
break;
else
printf ( " \n\n");
}
while(Ans != 'y' || Ans != 'Y');
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf ( "\t\t >> ...Thank YOU For Using OUR Audio System... >> \n\n");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf ( "\t\t To Exit To The Main Menu Press Any Key to Exit...");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
getch();
}
}
thanks
enashre@yahoo.com
I made the following program with C language - but I do not know how to fix the error when the user enters characters instead of numbers;
For Example:
the values entered by the user must be numbers only not characters - and if he type character the program should give him error message says that " please enter numbers only"
how to do this.
appreciate any help ?
#include <stdio.h>
#include <conio.h>
float fs;
float t;
float f;
float q;
float c;
int x,i,a;
int S,M,H;
int main()
{
char Ans;
for (i=1;i<=5;i=i+a)
{
i--;
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" >> Welcome To Audio System >> \n");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" Please Enter: \n");
printf(" \n");
printf(" [1] To calculate File Size \n");
printf(" \n");
printf(" [2] To calculate Time \n");
printf(" \n");
printf(" [3] To calculate Frequnecy \n");
printf(" \n");
printf(" [4] To calculate Quantization \n");
printf(" \n");
printf(" [5] To calculate Channels \n");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
scanf("%d",&x);
if (x==1)
{
printf(" Please Enter Time \n Seconds\n ");
scanf("%d",&S);
printf(" \n");
printf(" Minutes\n ");
scanf("%d",&M);
printf(" \n");
printf(" Hours\n ");
scanf("%d",&H);
printf(" \n");
t=S+(M*60)+(H*60*60);
printf(" Please Enter Frequnecy \n ");
scanf("%f",&f);
printf(" \n");
printf(" Please Enter Quantization \n ");
scanf("%f",&q);
printf(" \n");
printf(" Please Enter Channels \n ");
scanf("%f",&c);
printf(" \n");
fs=t*(f*1000)*q*c;
fs=fs/8/1024/1024;
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The file size is %f MB \n",fs);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else if (x==2)
{ printf(" Please Enter \n File Size \n ");
scanf("%f",&fs);
printf(" Please Enter Frequnecy \n ");
scanf("%f",&f);
printf(" Please Enter Quantization \n ");
scanf("%f",&q);
printf(" Please Enter Channels \n ");
scanf("%f",&c);
t= fs/(f*q*c);
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The time is %f s \n",t);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else if (x==3)
{ printf(" Please Enter File Size \n ");
scanf("%f",&fs);
printf(" Please Enter Time \n Seconds\n ");
scanf("%d",&S);
printf(" Minutes\n ");
scanf("%d",&M);
printf(" Hours\n ");
scanf("%d",&H);
t=S+(M*60)+(H*60*60);
printf(" Please Enter Quantization \n ");
scanf("%f",&q);
printf(" Please Enter Channels \n ");
scanf("%f",&c);
f= (fs/(t*q*c))/1000;
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The Frequnecy is %f KHz \n",f);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else if (x==4)
{ printf(" Please Enter \n File Size \n ");
scanf("%f",&fs);
printf(" Please Enter Time \n Seconds\n ");
scanf("%d",&S);
printf(" Minutes\n ");
scanf("%d",&M);
printf(" Hours\n ");
scanf("%d",&H);
t=S+(M*60)+(H*60*60);
printf(" Please Enter Frequnecy \n ");
scanf("%f",&f);
printf(" Please Enter Channels \n ");
scanf("%f",&c);
q= fs/(t*f*c);
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The Quantization is %f \n",q);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else if (x==5)
{
printf(" Please Enter \n File Size \n ");
scanf("%f",&fs);
printf(" Please Enter Time \n Seconds\n ");
scanf("%d",&S);
printf(" Minutes\n ");
scanf("%d",&M);
printf(" Hours\n ");
scanf("%d",&H);
t=S+(M*60)+(H*60*60);
printf(" Please Enter Frequnecy \n ");
scanf("%f",&f);
printf(" Please Enter Quantization \n ");
scanf("%f",&q);
c= fs/(t*f*q);
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf(" The Channels is %f \n",c);
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
else
{
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf("\t\t\t\t \n Sorry you have typed wrong input\n \n ");
printf("\t\t\t\t \n Please Enter Correct Number from [1] TO [5] \n\n ");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
}
do
{
printf ("\t\t Please TYPE 'y' To Exit To The Main Menu ");
scanf ("%c", &Ans);
if(Ans == 'n' || Ans =='N')
printf("\t\t \n Could you Please Leave Me Now...\n\n", Ans);
else if(Ans == 'y' || Ans == 'Y')
break;
else
printf ( " \n\n");
}
while(Ans != 'y' || Ans != 'Y');
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf ( "\t\t >> ...Thank YOU For Using OUR Audio System... >> \n\n");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
printf(" \n");
printf ( "\t\t To Exit To The Main Menu Press Any Key to Exit...");
printf(" \n");
printf(" -------------------------------------------------------------------------- \n");
getch();
}
}
thanks
enashre@yahoo.com