Where is the mistake?

T

Tiny Tim

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

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Tiny said:
*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]
 
P

Peter

Tiny said:
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.
 
S

slebetman

Tiny said:
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".
 
M

Mark McIntyre

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
already found one problem.
 
T

Tiny Tim

Peter said:
scanf("%d%d", &a, &b);

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

#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.
 
K

Keith Thompson

Tiny Tim said:
Thanks but Sorry, it does not work as suggested.
I have removed the commas between %d.
Please see result below.
#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,
and the second reads them again, clobbering whatever values you read
the first time.

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

slebetman

Tiny said:
Thanks but Sorry, it does not work as suggested.
I have removed the commas between %d.
Please see result below.

#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;
}
 
E

Emmanuel Delahaye

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()...
 
T

Tiny Tim

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

Mark McIntyre

keith wrote
If you use Visual C++ as the platform for writing C, this message will
automatically come out at the end of every result.

No it won't. I've used Visual C for years, and neither VC6 nor .net
exhibits this behaviour - they both simply close the window when done.
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.

Only if you set some option in the VC or windows gui.

Keith's point was that the programme as posted didn't seem to be the
code as compiled.
 
K

Keith Thompson

Mark McIntyre said:
No it won't. I've used Visual C for years, and neither VC6 nor .net
exhibits this behaviour - they both simply close the window when done.

Only if you set some option in the VC or windows gui.

Keith's point was that the programme as posted didn't seem to be the
code as compiled.

No, that wasn't my point. According to Tiny Tim, the extra output
comes from the environment, not from the program itself. It's like
showing a shell prompt as part of the output on a Unix-like system.
 
M

Mark McIntyre

No, that wasn't my point. According to Tiny Tim, the extra output
comes from the environment, not from the program itself. It's like
showing a shell prompt as part of the output on a Unix-like system.

In that case, I misunderstood you. Sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top