C language programming

Status
Not open for further replies.

sameissa

Right off the assembly line
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
 

ayush_chh

Ambassador of Buzz
you can check the ascii value of the character that is enterd by the user if it is any thing below 48 and above 57 then you can display msg " pls enter numbers only"
 
^^ Follow the first two advices. And please do not post in such big bold red letters. I think you are not familiar with manners while working on public forums. Dont mind, just a humble advice, no offences.
 

Edge-of-chaos

Broken In
A long way out could be using a loop, using ascii to test if the user input contains a character. However I too think the isalpha thing could work.
 

Edge-of-chaos

Broken In
this link is good, but doesn't seem to have an example. Could help if theres a sample validation code that someone could share or point to
 

aditya.shevade

Console Junkie
Most simple method. Use edit sets... now google to know what is edit set. OR, search in forum. Some other aditya has posted a tut about it...
 

aditya.shevade

Console Junkie
^^ Okay... I did the searching. And guess what, I found it in the forum as I said. Look for the other adityas in the forum.

Check this first,

*www.thinkdigit.com/forum/showthread.php?t=71047&highlight=edit+set

then

*blog.aditech.info/edit-set

Enjoy....
 
Status
Not open for further replies.
Top Bottom