-
July 13th, 2004, 11:02 AM
#1
Senior Member
scanf not scanning
hello guys, i am facing the problem, it is a very simple one, but the solution is not striking right now, the following is the code that i have typed, but there is a problem in scanning the value of character variable 'chy', the compiler is not stopping to accept the value.
the scanf line which is bold and in italics is the one with the problem, i think so. following is the code.
-------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
int main()
{
double pie,theth,dis,velo=0.0;
char chy;
char ty;
do
{
printf("\nEnter the angle theta: ");
scanf("%lg",&theth);
printf("\n Enter the angle pie: ");
scanf("%lg",&pie);
printf("\nEnter the velocity of the projectile: ");
scanf("%lg",&velo);
pie=3.1415927/180.0*pie;
theth=3.1415927/180.0*theth;
dis=2.0*velo*velo*cos(theth)*cos(theth)*(tan(theth)-tan(pie)/(9.81 * cos(pie)));
printf("\nThe Distance is: %g",dis);
printf("\nDo You whish for another calculations [Y|N]: ");
scanf("%c", &chy);
printf("\nthe valu of n and N is: %c, %c",chy,ty);
}while((chy != 144) || (chy != 78));
return 0;
}
-----------------------------------------------------------------------------------------------------------
please let me know about the problem.
thank you for your time and patience.
Regards
Harbir
U get What U pay for.
-
July 13th, 2004, 03:41 PM
#2
I might be wrong, I haven't done C in a very long time. But shouldn't that be ch, and not C?
-
July 13th, 2004, 04:08 PM
#3
no, the line is correct, but i've found exactly the same problem from time to time, if i use scan in an if() statement, i also have the same problem with char.
why don't you try:
chy = getchar();
instead of your line?
-
July 13th, 2004, 06:05 PM
#4
I was just about to say the same thing. scanf() is a very fussy function. Sometimes, it just refuses to scan. To get single letters from input, I find getchar() to be much much better.
Cheers,
cgkanchi
-
July 13th, 2004, 06:42 PM
#5
Hmm a quick question.. if somebody were to insert more than one character, wouldn't the next chars be passed on to whatever other function would 'buy' them? This could result in an overflow. I think it does depend on the compiler because on some I did have that problem, while on others I didn't.
But yeah, your best bet would be to do a getchar(), it should simplify things greatly.
/ \\
-
July 13th, 2004, 07:04 PM
#6
hi
here you go...cant use Characters like that........... use String instead .....or the .....getche() method ...getche() looks more ...cool ...you don't have to press Return after entering the Character..I have included both methods...and a few alteratio to the code hope you don't mind ...the code i have altered or added i have made it Bold and Italics
It's been a long since i worked on C ..it was a good feeling after that long time......
.Code works on Turbo C/C++..... Visual C++ ....can't say about other compilers...
Code:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
double pie,theth,dis,velo=0.0;
char ch;
do
{
printf("\nEnter the angle theta: ");
scanf("%lg",&theth);
printf("\n Enter the angle pie: ");
scanf("%lg",&pie);
printf("\nEnter the velocity of the projectile: ");
scanf("%lg",&velo);
pie=3.1415927/180.0*pie;
theth=3.1415927/180.0*theth;
dis=2.0*velo*velo*cos(theth)*cos(theth)*(tan(theth)-tan(pie)/(9.81 * cos(pie)));
printf("The Distance is:%lg\n",dis);
printf("\nContinue? Y/N: ");
scanf("%s",&ch);
//ch=getche();
}while(toupper(ch)=='Y');
getch();
return 0;
}
--Good Luck--
-
July 13th, 2004, 07:31 PM
#7
AFAIK, getch() and getche() aren't ANSI C functions. Therefore, you are not guaranteed that they will work. I know for a fact that it doesn't work on gcc and that it works on Turbo/Borland C++.
Cheers,
cgkanchi
-
July 13th, 2004, 08:32 PM
#8
Senior Member
Thanx Guys,
the code works well with %s after including #inlcude<stdio.h>
cheers
U get What U pay for.
-
July 13th, 2004, 08:43 PM
#9
Hi
Yes Antionline Herpetologist is right they are not ..i have always worked on Turbo C ++ that i sometimes forget that there are other Compilers as well and about the standard ANSI C functions.
The Above Code will work without them ...Just delete the Getch(); at the end ...its of no use there ..i write that as a habit .....otherwise the DOS window Dissapers without even letting me see the result....it's just there to stop it after the whole program is finished .
--Good Luck--
-
July 13th, 2004, 10:06 PM
#10
Thanx Guys,
the code works well with %s after including #inlcude<stdio.h>
cheers
the code you've printed in your first post, already had "#include <stdio.h>" in it, so this makes no sense
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|