scanf and c++

Discussion in 'C++' started by Xavier Serrand, Jul 15, 2007.

  1. With GCC version 3.4.4 (mingw special) , following code gives stange results
    :


    void encapsulation_c(void)
    {
    double x, y;
    std::printf("Calcul de moyenne\n");
    std::printf("Entrez le premier nombre : ");
    std::scanf("%f", &x);
    std::printf("\nEntrez le deuxieme nombre : ");
    std::scanf("%f", &y);
    //y = 3;
    std::printf("\n\nLa valeur moyenne de %8.2f et de %8.2f est %8.2f\n",
    x, y, ((x+y)/2));
    }

    output is :

    Calcul de moyenne
    Entrez le premier nombre : 1.0

    Entrez le deuxieme nombre : 2.0


    La valeur moyenne de 0.00 et de 0.00 est 0.00


    Some idea ?
     
    Xavier Serrand, Jul 15, 2007
    #1
    1. Advertising

  2. Xavier Serrand wrote:

    > With GCC version 3.4.4 (mingw special) , following code gives stange
    > results
    > :
    >
    >
    > void encapsulation_c(void)
    > {
    > double x, y;
    > std::printf("Calcul de moyenne\n");
    > std::printf("Entrez le premier nombre : ");
    > std::scanf("%f", &x);


    std::scanf("%lf", &x);

    > std::printf("\nEntrez le deuxieme nombre : ");
    > std::scanf("%f", &y);


    std::scanf("%lf", &y);

    > //y = 3;
    > std::printf("\n\nLa valeur moyenne de %8.2f et de %8.2f est %8.2f\n",
    > x, y, ((x+y)/2));
    > }
    >
    > output is :
    >
    > Calcul de moyenne
    > Entrez le premier nombre : 1.0
    >
    > Entrez le deuxieme nombre : 2.0
    >
    >
    > La valeur moyenne de 0.00 et de 0.00 est 0.00
    >
    >
    > Some idea ?


    Read the manual when using printf/scanf. Googling for scanf says exactly
    what's needed. Also, the warning

    warning: format '%f' expects type 'float*', but argument 2 has
    type 'double*'

    is also useful.

    --
    rbh
     
    Robert Bauck Hamar, Jul 15, 2007
    #2
    1. Advertising

  3. Xavier Serrand

    James Kanze Guest

    On Jul 15, 8:33 pm, "Xavier Serrand" <> wrote:
    > With GCC version 3.4.4 (mingw special) , following code gives stange results
    > :


    > void encapsulation_c(void)
    > {
    > double x, y;
    > std::printf("Calcul de moyenne\n");
    > std::printf("Entrez le premier nombre : ");
    > std::scanf("%f", &x);
    > std::printf("\nEntrez le deuxieme nombre : ");
    > std::scanf("%f", &y);
    > //y = 3;
    > std::printf("\n\nLa valeur moyenne de %8.2f et de %8.2f est %8.2f\n",
    > x, y, ((x+y)/2));
    > }


    > output is :


    > Calcul de moyenne
    > Entrez le premier nombre : 1.0


    > Entrez le deuxieme nombre : 2.0


    > La valeur moyenne de 0.00 et de 0.00 est 0.00


    > Some idea ?


    The code has undefined behavior, so nothing can really be
    considered strange.

    Forget about scanf. It's very difficult to use correctly to
    begin with, and very, very fragile, leading to absolutely
    unmaintainable code. Something like:

    double x, y;
    std::cout << "Calcul de moyenne" << std::endl ;
    std::cout << "Entrez le premier nombre : " ;
    std::cin >> x ;
    std::cout << "\nEntrez le deuxieme nombre : " ;
    std::cin >> y ;

    Does the trick nicely (except that it lacks error handling), and
    is robust in face of change. (It works equally well, regardless
    of whether x and y are double, float or even int.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 16, 2007
    #3
    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. Replies:
    5
    Views:
    3,098
    Richard Herring
    Aug 4, 2004
  2. Rob

    help with infinite loops and scanf

    Rob, Jul 26, 2003, in forum: C Programming
    Replies:
    8
    Views:
    799
    Peter Shaggy Haywood
    Jul 29, 2003
  3. Eirik

    fgets() and scanf() peculiarity

    Eirik, Dec 10, 2003, in forum: C Programming
    Replies:
    6
    Views:
    979
    Richard Heathfield
    Dec 12, 2003
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,515
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    703
    Richard Bos
    May 2, 2006
Loading...

Share This Page