473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Where is the mistake?

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.

Dec 3 '05 #1
12 1797
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]
Dec 3 '05 #2

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.


Dec 3 '05 #3
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".

Dec 3 '05 #4
On Sat, 3 Dec 2005 19:46:19 +0800, in comp.lang.c , "Tiny Tim"
<ts*****@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 =----
Dec 3 '05 #5

"Peter" <co****@gmail.com> wrote in message news:11**********************@g47g2000cwa.googlegr 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.
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.

Dec 4 '05 #6
"Tiny Tim" <ts*****@streamyx.com> writes:
"Peter" <co****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr 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.
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) ks***@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.
Dec 4 '05 #7
Tiny Tim wrote:
"Peter" <co****@gmail.com> wrote in message news:11**********************@g47g2000cwa.googlegr 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.
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;
}

Dec 4 '05 #8
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
Dec 4 '05 #9
> 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.
Dec 4 '05 #10
On Mon, 5 Dec 2005 00:29:16 +0800, in comp.lang.c , "Tiny Tim"
<ts*****@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 willclose 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 =----
Dec 4 '05 #11
Mark McIntyre <ma**********@spamcop.net> writes:
On Mon, 5 Dec 2005 00:29:16 +0800, in comp.lang.c , "Tiny Tim"
<ts*****@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) ks***@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.
Dec 4 '05 #12
On Sun, 04 Dec 2005 19:56:34 GMT, in comp.lang.c , Keith Thompson
<ks***@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 =----
Dec 5 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul T. Rong | last post by:
Dear all, My aim is to compact and repair current database, I got the following code from http://www.mvps.org/access/general/gen0041.htm Option Compare Database ' ***** Code Start *****...
15
by: Paul T. RONG | last post by:
Hello, I am making a restaurant database (it is much more complicated than I thought before!), now it comes to the last stage and I come across a problem. I will explain it in detail. In a...
25
by: Nikola | last post by:
compiler says: function undeclared how come??? help! #include<stdio.h> #include<stdlib.h> #include<time.h> struct lista{ int element; struct lista *next; }*pocetak;
23
by: Red Dragon | last post by:
I am self study C program student. Hope someone can help me with this problem. This program generates random numbers over a user defined range using call function I used the call function "...
3
by: bughunter | last post by:
IMHO, statements like this is mistake typically. May be more better made this construction - I said about empty WHERE - invalid? A lot of data will saved... :-) Andy
2
by: Lad | last post by:
I use the following code to sort dictionary. Olddict={'r':4,'c':1,'d':2,'e':3,'f':2} Newdict={} i = i.sort() # by val i.reverse() # Get largest first. for (val, key) in i: print key,val
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
4
by: Winston | last post by:
Where is the mistake? I want to make a simple menu. These are two pieces of two files... function ShowMenu(objeto) { is_open = document.getElementById(objeto).style.display;...
4
by: | last post by:
I have learned about compartmentalizing my code base using Class Libraries. I have my common code such as my ORM framework broken out into their own Class Libraries, which are referenced as...
2
by: Eglute | last post by:
Hello. I have a problem. I am a begginer in PHP. I wrote the code: <? $variable=5; $variable<10? {$ans="less"; echo $ans."<br>"; echo "variable=".$variable."<br>";}: ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.