Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 06-10-2008, 03:59 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Sep 2008
Posts: 3
Smile C language programming


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
sameissa is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 06-10-2008, 09:01 PM   #2 (permalink)
In The Zone
 
ayush_chh's Avatar
 
Join Date: Nov 2005
Location: Bangalore
Posts: 487
Default Re: C language programming

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"
__________________
eXPerience is what a MAN learn's fROM.....
ayush_chh is offline  
Old 14-10-2008, 01:52 AM   #3 (permalink)
NULL_PTR
 
Join Date: Jan 2006
Location: Dangling Pointer
Posts: 88
Default Re: C language programming

first use code tags
then i think there is a function
int isalpha ( int c ); read more here
and plz use google
__________________
Writing Mine Own Os
Wanna Join Pm ME
umeshtangnu is offline  
Old 14-10-2008, 04:46 AM   #4 (permalink)
Alpha Geek
 
Krazy_About_Technology's Avatar
 
Join Date: Jun 2004
Location: Noida - India
Posts: 765
Default Re: C language programming

^^ 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.
__________________
Dell Inspiron 1525 - C2D 2 Ghz, 3GB, 250GB, X3100 :)

Samsung Omnia Pro B7610 with Stock WM 6.1 ROM

Blog: http://www.sumitbhardwaj.co.in/blog
Krazy_About_Technology is offline  
Old 14-10-2008, 02:55 PM   #5 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: C language programming

I'll go with umeshtangu, there is a isalpha() in ctype.h, you might want to use that.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 14-10-2008, 08:12 PM   #6 (permalink)
Right Off the Assembly Line
 
Join Date: Sep 2008
Posts: 18
Default Re: C language programming

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 is offline  
Old 15-10-2008, 06:38 AM   #7 (permalink)
j_h
Banned
 
Join Date: Jun 2007
Posts: 105
Default Re: C language programming

http://www.opengroup.org/onlinepubs/...s/isalpha.html
j_h is offline  
Old 15-10-2008, 08:23 PM   #8 (permalink)
Right Off the Assembly Line
 
Join Date: Sep 2008
Posts: 18
Default Re: C language programming

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
Edge-of-chaos is offline  
Old 16-10-2008, 11:27 AM   #9 (permalink)
Right Off the Assembly Line
 
Join Date: Nov 2006
Posts: 11
Default Re: C language programming

use "switch" statement instead of "if" and display a massege "not a valid choice" in default case.
dkverma87 is offline  
Old 18-10-2008, 01:59 AM   #10 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: C language programming

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...
__________________
--- Console Junkie
aditya.shevade is offline  
Old 18-10-2008, 06:05 PM   #11 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: C language programming

^^ What are edit sets? googling didnt produce any results. The term is too generic.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 19-10-2008, 12:57 AM   #12 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: C language programming

^^ 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,

Lesser known facts in C

then

http://blog.aditech.info/edit-set

Enjoy....
__________________
--- Console Junkie
aditya.shevade is offline  
Old 19-10-2008, 12:32 PM   #13 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: C language programming

^^ Thanks! I saved the entire thread - looks like it could be useful!
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 19-10-2008, 09:38 PM   #14 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: C language programming

It is helpful... a lot.
__________________
--- Console Junkie
aditya.shevade is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which Programming Language i should use? Thegame Programming 4 16-02-2008 11:47 AM
Help me choose a programming language ring_wraith Programming 5 10-01-2008 01:45 PM
Best Programming Language nepcker Programming 8 23-05-2007 07:25 PM
In which programming language? manas Programming 2 27-05-2006 05:37 PM
best language for sockets programming!! Ashootosh Networking 5 03-02-2005 01:50 PM

 
Latest Threads
- by clinton
- by soumya
- by Sujeet
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 11:14 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2