scanf and c++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xavier Serrand

    #1

    scanf and c++

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


    void encapsulation_c (void)
    {
    double x, y;
    std::printf("Ca lcul de moyenne\n");
    std::printf("En trez le premier nombre : ");
    std::scanf("%f" , &x);
    std::printf("\n Entrez 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 ?


  • Robert Bauck Hamar

    #2
    Re: scanf and c++

    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("Ca lcul de moyenne\n");
    std::printf("En trez le premier nombre : ");
    std::scanf("%f" , &x);
    std::scanf("%lf ", &x);
    std::printf("\n Entrez 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

    Comment

    • James Kanze

      #3
      Re: scanf and c++

      On Jul 15, 8:33 pm, "Xavier Serrand" <xxxxxx.xxxx... @xxx.frwrote:
      With GCC version 3.4.4 (mingw special) , following code gives stange results
      :
      void encapsulation_c (void)
      {
      double x, y;
      std::printf("Ca lcul de moyenne\n");
      std::printf("En trez le premier nombre : ");
      std::scanf("%f" , &x);
      std::printf("\n Entrez 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:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      Working...