Where is the mistake?

Discussion in 'C Programming' started by Tiny Tim, Dec 3, 2005.

  1. Tiny Tim

    Tiny Tim Guest

    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.
     
    Tiny Tim, Dec 3, 2005
    #1
    1. Advertising

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


    [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]
     
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Dec 3, 2005
    #2
    1. Advertising

  3. Tiny Tim

    Peter Guest

    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.
     
    Peter, Dec 3, 2005
    #3
  4. Tiny Tim

    Guest

    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, 2005
    #4
  5. On Sat, 3 Dec 2005 19:46:19 +0800, in comp.lang.c , "Tiny Tim"
    <> 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 =----
     
    Mark McIntyre, Dec 3, 2005
    #5
  6. Tiny Tim

    Tiny Tim Guest

    "Peter" <> wrote in message news:...
    >
    > 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.
     
    Tiny Tim, Dec 4, 2005
    #6
  7. "Tiny Tim" <> writes:
    > "Peter" <> wrote in message
    > news:...
    >>
    >> 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) <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.
     
    Keith Thompson, Dec 4, 2005
    #7
  8. Tiny Tim

    Guest

    Tiny Tim wrote:
    > "Peter" <> wrote in message news:...
    > >
    > > 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, 2005
    #8
  9. 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
     
    Emmanuel Delahaye, Dec 4, 2005
    #9
  10. Tiny Tim

    Tiny Tim Guest

    > 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.
     
    Tiny Tim, Dec 4, 2005
    #10
  11. On Mon, 5 Dec 2005 00:29:16 +0800, in comp.lang.c , "Tiny Tim"
    <> 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 =----
     
    Mark McIntyre, Dec 4, 2005
    #11
  12. Mark McIntyre <> writes:
    > On Mon, 5 Dec 2005 00:29:16 +0800, in comp.lang.c , "Tiny Tim"
    > <> 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) <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.
     
    Keith Thompson, Dec 4, 2005
    #12
  13. On Sun, 04 Dec 2005 19:56:34 GMT, in comp.lang.c , Keith Thompson
    <> 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 =----
     
    Mark McIntyre, Dec 5, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. \(beta-\) Frank Nitzsche

    where is the mistake?

    \(beta-\) Frank Nitzsche, Jun 25, 2004, in forum: VHDL
    Replies:
    4
    Views:
    556
  2. Jesse

    Beginner Mistake

    Jesse, Jul 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    377
    Jesse
    Jul 22, 2003
  3. Joerg Jooss
    Replies:
    1
    Views:
    420
    DotNetJunkies User
    May 22, 2004
  4. John
    Replies:
    6
    Views:
    675
  5. Leon

    Does anyone see my mistake?

    Leon, Nov 10, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    349
Loading...

Share This Page