scanf not scanning

1. ## 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&lt;stdio.h&gt;

#include&lt;math.h&gt;
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;
}

-----------------------------------------------------------------------------------------------------------
thank you for your time and patience.
Regards
Harbir

2. I might be wrong, I haven't done C in a very long time. But shouldn't that be ch, and not C?

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();

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

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.

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&lt;stdio.h&gt;
#include&lt;string.h&gt;
#include&lt;math.h&gt;
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--

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

8. Thanx Guys,
the code works well with %s after including #inlcude&lt;stdio.h&gt;

cheers

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

10. Thanx Guys,
the code works well with %s after including #inlcude&lt;stdio.h&gt;

cheers
the code you've printed in your first post, already had "#include &lt;stdio.h&gt;" in it, so this makes no sense

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•