DIGIT-ARC read-only archive · snapshot: July 23, 2026

C language programming

14 posts · originally from https://geek.digit.in/community/threads/c-language-programming.99224/

S
sameissa Original post #1
Oct 6, 2008 at 3:59 PM
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
Oct 6, 2008 at 9:01 PM
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"
U
Oct 14, 2008 at 1:52 AM
first use code tags
then i think there is a function
int isalpha ( int c ); read more here
and plz use google
Krazy_About_Technology
Oct 14, 2008 at 4:46 AM
^^ 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.
dheeraj_kumar
Oct 14, 2008 at 2:55 PM
I'll go with umeshtangu, there is a isalpha() in ctype.h, you might want to use that.
E
Oct 14, 2008 at 8:12 PM
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.
J
j_h #7
Oct 15, 2008 at 6:38 AM
*www.opengroup.org/onlinepubs/009695399/functions/isalpha.html
E
Oct 15, 2008 at 8:23 PM
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
D
Oct 16, 2008 at 11:27 AM
use "switch" statement instead of "if" and display a massege "not a valid choice" in default case.
aditya.shevade
Oct 18, 2008 at 1:59 AM
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...
dheeraj_kumar
Oct 18, 2008 at 6:05 PM
^^ What are edit sets? googling didnt produce any results. The term is too generic.
aditya.shevade
Oct 19, 2008 at 12:57 AM
^^ 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....
dheeraj_kumar
Oct 19, 2008 at 12:32 PM
^^ Thanks! I saved the entire thread - looks like it could be useful! :)