Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   scanf and c++ (http://www.velocityreviews.com/forums/t522124-scanf-and-c.html)

Xavier Serrand 07-15-2007 06:33 PM

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("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 ?



Robert Bauck Hamar 07-15-2007 06:50 PM

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("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

James Kanze 07-16-2007 07:29 AM

Re: scanf and c++
 
On Jul 15, 8:33 pm, "Xavier Serrand" <xxxxxx.xxxx...@xxx.fr> 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:james.kanze@gmail.com
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



All times are GMT. The time now is 02:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.