This is a multi-part message in MIME format.
------=_NextPart_000_000C_01C5F842.3B3CFDB0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
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. =20
#include <stdio.h>
int main ()
{
int a,i;
=20
for (i=3D1;i<=3D3;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.=20
Case 2
#include <stdio.h>
int main ()
{
int a,b,i;
=20
for (i=3D1;i<=3D3;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=20
Khoon.
------=_NextPart_000_000C_01C5F842.3B3CFDB0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2769" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><STRONG>Hope someone can help me to =
overcome this=20
problem.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Case 1 is working =
perfectly working=20
with one number entry.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Case 2 is the problem when I =
tried with 2=20
numbers entry.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#008080 size=3D2><STRONG>Case 1. =20
</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#008080 size=3D2><STRONG>#include=20
<stdio.h><BR>int main ()<BR>{<BR> int =
a,i;<BR> =20
<BR> for (i=3D1;i<=3D3;i++)<BR> {<BR> scanf=20
("%d",&a);<BR> printf ("%d\n",a);<BR> }<BR> return=20
0;<BR>}<BR>/* Result<BR>5<BR>5<BR>15<BR>15<BR>234<BR>234<BR>Press any =
key to=20
continue<BR>*/</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>As the result shows, I =
enter 5 and=20
computer prints 5,</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>enter 15 and computer prints=20
15 , 234 and prints 234. No problem with=20
this.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG></STRONG></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>The problem comes when I deal =
with 2=20
numbers, a and b. </STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial color=3D#008080 size=3D2><STRONG>Case =
2</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#008080 size=3D2><STRONG>#include=20
<stdio.h><BR>int main ()<BR>{<BR> int =
a,b,i;<BR> =20
<BR> for (i=3D1;i<=3D3;i++)<BR> {<BR> scanf=20
("%d,%d",&a,&b);<BR> printf=20
("%d,%d\n",a,b);<BR> }<BR> return 0;<BR>}<BR>/* Result<BR>1=20
2<BR>1,-858993460<BR>2,-858993460<BR>3 4<BR>3,-858993460<BR>Press any =
key to=20
continue<BR>*/</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>As you can see from the above=20
result, when I enter 1 2, I get weird result. I =
suspect =20
the computer is printing "space" and "enter key".</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>How do I overcome this=20
problem?</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG></STRONG></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Thanks </STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Khoon.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2><STRONG></STRONG></FONT> </DIV></BODY></HTML>
------=_NextPart_000_000C_01C5F842.3B3CFDB0--
|
|
0
|
|
|
|
Reply
|
tskhoon (58)
|
12/3/2005 11:46:19 AM |
|
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]
|
|
0
|
|
|
|
Reply
|
boa11 (125)
|
12/3/2005 12:00:31 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
coscpp (8)
|
12/3/2005 1:58:51 PM
|
|
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".
|
|
0
|
|
|
|
Reply
|
slebetman (894)
|
12/3/2005 4:25:33 PM
|
|
On Sat, 3 Dec 2005 19:46:19 +0800, in comp.lang.c , "Tiny Tim"
<tskhoon@streamyx.com> 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
already found one problem.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== 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 =----
|
|
0
|
|
|
|
Reply
|
markmcintyre (4547)
|
12/3/2005 6:31:34 PM
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_00EA_01C5F8C2.99EC6F00
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
"Peter" <coscpp@gmail.com> wrote in message =
news:1133618331.244601.309110@g47g2000cwa.googlegroups.com...
>=20
> 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.
Please see result below.
#include <stdio.h>
int main ()
{
int a,b,i;
=20
for (i=3D1;i<=3D3;i++)
{
scanf ("%d %d",&a,&b);
scanf ("%d %d",&a,&b);
=20
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.
------=_NextPart_000_00EA_01C5F8C2.99EC6F00
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2769" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>"Peter" <</FONT><A=20
href=3D"mailto:coscpp@gmail.com"><FONT face=3DArial=20
size=3D2>coscpp@gmail.com</FONT></A><FONT face=3DArial size=3D2>> =
wrote in message=20
</FONT><A=20
href=3D"news:1133618331.244601.309110@g47g2000cwa.googlegroups.com"><FONT=
=20
face=3DArial=20
size=3D2>news:1133618331.244601.309110@g47g2000cwa.googlegroups.com</FONT=
></A><FONT=20
face=3DArial size=3D2>...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>> <BR>> Tiny Tim =
wrote:<BR>>> =20
scanf ("%d,%d",&a,&b);<BR>> scanf("%d%d", &a,=20
&b);</FONT></DIV><FONT face=3DArial size=3D2>
<DIV><BR><STRONG>Thanks but Sorry, it does not work as=20
suggested.</STRONG></DIV>
<DIV><STRONG>I have removed the commas between %d.</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Please see result=20
below.</STRONG></FONT></DIV><FONT face=3DArial size=3D2></FONT>
<DIV><FONT face=3DArial size=3D2></FONT><BR><FONT face=3DArial =
color=3D#008080=20
size=3D2><STRONG>#include <stdio.h><BR>int main =
()<BR>{<BR> int=20
a,b,i;<BR> <BR> for=20
(i=3D1;i<=3D3;i++)<BR> {<BR> scanf ("%d=20
%d",&a,&b);<BR> scanf ("%d=20
%d",&a,&b);<BR> <BR> printf ("%d=20
%d\n",a,b);<BR> }<BR> return 0;<BR>}<BR>/* Result<BR>1 2<BR>3 =
4<BR>3=20
4<BR>5 6<BR>7 8<BR>7 8<BR>9 10<BR>11 12<BR>11 12<BR>Press any key to=20
continue<BR>*/</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>It works only on alternative =
entries, which=20
is not correct.</STRONG></FONT></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG> </DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>Regards,</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2>Khoon.</FONT></STRONG></DIV>
<DIV><STRONG><FONT face=3DArial size=3D2></FONT></STRONG> </DIV>
<DIV><FONT face=3DArial size=3D2><STRONG></STRONG></FONT> </DIV>
<DIV><FONT face=3DArial =
size=3D2><STRONG></STRONG></FONT> </DIV></BODY></HTML>
------=_NextPart_000_00EA_01C5F8C2.99EC6F00--
|
|
0
|
|
|
|
Reply
|
tskhoon (58)
|
12/4/2005 3:05:12 AM
|
|
"Tiny Tim" <tskhoon@streamyx.com> writes:
> "Peter" <coscpp@gmail.com> wrote in message
> news:1133618331.244601.309110@g47g2000cwa.googlegroups.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.
> 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?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.
|
|
0
|
|
|
|
Reply
|
kst-u (21460)
|
12/4/2005 4:25:43 AM
|
|
Tiny Tim wrote:
> "Peter" <coscpp@gmail.com> wrote in message news:1133618331.244601.309110@g47g2000cwa.googlegroups.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.
> 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;
}
|
|
0
|
|
|
|
Reply
|
slebetman (894)
|
12/4/2005 11:48:43 AM
|
|
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
|
|
0
|
|
|
|
Reply
|
emdel (952)
|
12/4/2005 1:01:41 PM
|
|
> 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.
|
|
0
|
|
|
|
Reply
|
tskhoon (58)
|
12/4/2005 4:29:16 PM
|
|
On Mon, 5 Dec 2005 00:29:16 +0800, in comp.lang.c , "Tiny Tim"
<tskhoon@streamyx.com> wrote:
>keith wrote
>> BTW, where does the "Press any key to continue" message come from?
>>
>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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== 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 =----
|
|
0
|
|
|
|
Reply
|
markmcintyre (4547)
|
12/4/2005 5:52:38 PM
|
|
Mark McIntyre <markmcintyre@spamcop.net> writes:
> On Mon, 5 Dec 2005 00:29:16 +0800, in comp.lang.c , "Tiny Tim"
> <tskhoon@streamyx.com> wrote:
>
>>keith wrote
>>> BTW, where does the "Press any key to continue" message come from?
>>>
>>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.
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.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.
|
|
0
|
|
|
|
Reply
|
kst-u (21460)
|
12/4/2005 7:56:34 PM
|
|
On Sun, 04 Dec 2005 19:56:34 GMT, in comp.lang.c , Keith Thompson
<kst-u@mib.org> wrote:
>
>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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== 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 =----
|
|
0
|
|
|
|
Reply
|
markmcintyre (4547)
|
12/5/2005 12:03:43 AM
|
|
|
12 Replies
33 Views
(page loaded in 0.169 seconds)
|