Velocity Reviews > Where is the mistake?

# Where is the mistake?

Tiny Tim
Guest
Posts: n/a

 12-03-2005
Hope someone can help me to overcome this problem.
Case 1 is working perfectly working with one number entry.
Case 2 is the problem when I tried with 2 numbers entry.
Case 1.
#include <stdio.h>
int main ()
{
int a,i;

for (i=1;i<=3;i++)
{
scanf ("%d",&a);
printf ("%d\n",a);
}
return 0;
}
/* Result
5
5
15
15
234
234
Press any key to continue
*/
As the result shows, I enter 5 and computer prints 5,
enter 15 and computer prints 15 , 234 and prints 234. No problem with this.

The problem comes when I deal with 2 numbers, a and b.

Case 2
#include <stdio.h>
int main ()
{
int a,b,i;

for (i=1;i<=3;i++)
{
scanf ("%d,%d",&a,&b);
printf ("%d,%d\n",a,b);
}
return 0;
}
/* Result
1 2
1,-858993460
2,-858993460
3 4
3,-858993460
Press any key to continue
*/

As you can see from the above result, when I enter 1 2, I get weird result. I suspect the computer is printing "space" and "enter key".
How do I overcome this problem?

Thanks
Khoon.

Guest
Posts: n/a

 12-03-2005
Tiny Tim wrote:
> *Hope someone can help me to overcome this problem.*
> *Case 1 is working perfectly working with one number entry.*
> *Case 2 is the problem when I tried with 2 numbers entry.*
> *Case 1. *

[snip]

>
> *Case 2*
> *#include <stdio.h>
> int main ()
> {
> int a,b,i;
>
> for (i=1;i<=3;i++)
> {
> scanf ("%d,%d",&a,&b);

scanf ("%d %d",&a,&b);

Bjørn
[snip]

Peter
Guest
Posts: n/a

 12-03-2005

Tiny Tim wrote:
> Hope someone can help me to overcome this problem.
> Case 1 is working perfectly working with one number entry.
> Case 2 is the problem when I tried with 2 numbers entry.
> Case 1.
> #include <stdio.h>
> int main ()
> {
> int a,i;
>
> for (i=1;i<=3;i++)
> {
> scanf ("%d",&a);
> printf ("%d\n",a);
> }
> return 0;
> }
> /* Result
> 5
> 5
> 15
> 15
> 234
> 234
> Press any key to continue
> */
> As the result shows, I enter 5 and computer prints 5,
> enter 15 and computer prints 15 , 234 and prints 234. No problem with this.
>
> The problem comes when I deal with 2 numbers, a and b.
>
> Case 2
> #include <stdio.h>
> int main ()
> {
> int a,b,i;
>
> for (i=1;i<=3;i++)
> {
> scanf ("%d,%d",&a,&b);

scanf("%d%d", &a, &b);
> printf ("%d,%d\n",a,b);
> }
> return 0;
> }
> /* Result
> 1 2
> 1,-858993460
> 2,-858993460
> 3 4
> 3,-858993460
> Press any key to continue
> */
>
> As you can see from the above result, when I enter 1 2, I get weird result. I suspect the computer is printing "space" and "enter key".
> How do I overcome this problem?
>
> Thanks
> Khoon.

slebetman@yahoo.com
Guest
Posts: n/a

 12-03-2005
Tiny Tim wrote:
> Hope someone can help me to overcome this problem.
> Case 1 is working perfectly working with one number entry.
> Case 2 is the problem when I tried with 2 numbers entry.
> Case 1.
> #include <stdio.h>
> int main ()
> {
> int a,i;
>
> for (i=1;i<=3;i++)
> {
> scanf ("%d",&a);
> printf ("%d\n",a);
> }
> return 0;
> }
> /* Result
> 5
> 5
> 15
> 15
> 234
> 234
> Press any key to continue
> */
> As the result shows, I enter 5 and computer prints 5,
> enter 15 and computer prints 15 , 234 and prints 234. No problem with this.
>
> The problem comes when I deal with 2 numbers, a and b.
>
> Case 2
> #include <stdio.h>
> int main ()
> {
> int a,b,i;
>
> for (i=1;i<=3;i++)
> {
> scanf ("%d,%d",&a,&b);
> printf ("%d,%d\n",a,b);
> }
> return 0;
> }
> /* Result
> 1 2
> 1,-858993460
> 2,-858993460
> 3 4
> 3,-858993460
> Press any key to continue
> */
>
> As you can see from the above result, when I enter 1 2, I get weird result. I suspect the computer is printing "space" and "enter key".
> How do I overcome this problem?
>

Don't enter 1 2. That's not what you programmed. Instead, enter 1,2
(which is "%d,%d").

If instead you intend to enter 1 2 do "%d %d".

Mark McIntyre
Guest
Posts: n/a

 12-03-2005
On Sat, 3 Dec 2005 19:46:19 +0800, in comp.lang.c , "Tiny Tim"
<(E-Mail Removed)> wrote:

> scanf ("%d,%d",&a,&b);

remove the comma in the format string.

Also, don't use scanf. Please read the FAQ for a discussion of why but
in summary its a dangerous function thats tricky to safely use. You
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Tiny Tim
Guest
Posts: n/a

 12-04-2005

"Peter" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) oups.com...
>
> Tiny Tim wrote:
>> scanf ("%d,%d",&a,&b);

> scanf("%d%d", &a, &b);

Thanks but Sorry, it does not work as suggested.
I have removed the commas between %d.

#include <stdio.h>
int main ()
{
int a,b,i;

for (i=1;i<=3;i++)
{
scanf ("%d %d",&a,&b);
scanf ("%d %d",&a,&b);

printf ("%d %d\n",a,b);
}
return 0;
}
/* Result
1 2
3 4
3 4
5 6
7 8
7 8
9 10
11 12
11 12
Press any key to continue
*/
It works only on alternative entries, which is not correct.

Regards,
Khoon.

Keith Thompson
Guest
Posts: n/a

 12-04-2005
"Tiny Tim" <(E-Mail Removed)> writes:
> "Peter" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>>
>> Tiny Tim wrote:
>>> scanf ("%d,%d",&a,&b);

>> scanf("%d%d", &a, &b);

> Thanks but Sorry, it does not work as suggested.
> I have removed the commas between %d.
> #include <stdio.h>
> int main ()
> {
> int a,b,i;
>
> for (i=1;i<=3;i++)
> {
> scanf ("%d %d",&a,&b);
> scanf ("%d %d",&a,&b);
>
> printf ("%d %d\n",a,b);
> }
> return 0;
> }
> /* Result
> 1 2
> 3 4
> 3 4
> 5 6
> 7 8
> 7 8
> 9 10
> 11 12
> 11 12
> Press any key to continue
> */
> It works only on alternative entries, which is not correct.

Yes, it is. Well, it may not be correct, but it's what you asked for.

You have two calls to scanf. The first reads the values of a and b,
the first time.

BTW, where does the "Press any key to continue" message come from?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

slebetman@yahoo.com
Guest
Posts: n/a

 12-04-2005
Tiny Tim wrote:
> "Peter" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) oups.com...
> >
> > Tiny Tim wrote:
> >> scanf ("%d,%d",&a,&b);

> > scanf("%d%d", &a, &b);

>
> Thanks but Sorry, it does not work as suggested.
> I have removed the commas between %d.
>
> #include <stdio.h>
> int main ()
> {
> int a,b,i;
>
> for (i=1;i<=3;i++)
> {
> scanf ("%d %d",&a,&b);
> scanf ("%d %d",&a,&b);
>
> printf ("%d %d\n",a,b);
> }
> return 0;
> }
> /* Result
> 1 2
> 3 4
> 3 4
> 5 6
> 7 8
> 7 8
> 9 10
> 11 12
> 11 12
> Press any key to continue
> */
> It works only on alternative entries, which is not correct.
>

Think about what you're doing to your variables. The error here is your
logic, not the code. Try the following:

#include <stdio.h>
int main ()
{
int a,b,i;

for (i=1;i<=3;i++)
{
scanf ("%d %d",&a,&b);
printf ("%d %d\n",a,b);

scanf ("%d %d",&a,&b);
printf ("%d %d\n",a,b);
}
return 0;
}

Or better yet, the following:

#include <stdio.h>
int main ()
{
int a,b,i;

for (i=1;i<=3;i++)
{
scanf ("%d %d",&a,&b);
printf ("%d %d\n",a,b);
}
return 0;
}

Emmanuel Delahaye
Guest
Posts: n/a

 12-04-2005
Tiny Tim a écrit :
> *#include <stdio.h>
> int main ()
> {
> int a,b,i;
>
> for (i=1;i<=3;i++)
> {
> scanf ("%d,%d",&a,&b);
> printf ("%d,%d\n",a,b);
> }
> return 0;
> }
> /* Result
> 1 2

Wrong. The expected separator is ','. You must type

1,2

Also, you should check that scanf() returns 2 and purge stdin if not...

Better to use fgest() and sscanf()...

--
A+

Emmanuel Delahaye

Tiny Tim
Guest
Posts: n/a

 12-04-2005
> BTW, where does the "Press any key to continue" message come from?
>

Dear Keith,
If you use Visual C++ as the platform for writing C, this message will
automatically come out at the end of every result.
This is because the result comes out in DOS mode. Pressing any key will
close the DOS window and return back to the Visual C++ format.
Thanks
Khoon.