Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > scanf and c++

Reply
Thread Tools

scanf and c++

 
 
Xavier Serrand
Guest
Posts: n/a
 
      07-15-2007
With GCC version 3.4.4 (mingw special) , following code gives stange results
:


void encapsulation_c(void)
{
double x, y;
std:rintf("Calcul de moyenne\n");
std:rintf("Entrez le premier nombre : ");
std::scanf("%f", &x);
std:rintf("\nEntrez le deuxieme nombre : ");
std::scanf("%f", &y);
//y = 3;
std:rintf("\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 ?


 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      07-15-2007
Xavier Serrand wrote:

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


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

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


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

> //y = 3;
> std:rintf("\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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      07-16-2007
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:rintf("Calcul de moyenne\n");
> std:rintf("Entrez le premier nombre : ");
> std::scanf("%f", &x);
> std:rintf("\nEntrez le deuxieme nombre : ");
> std::scanf("%f", &y);
> //y = 3;
> std:rintf("\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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
scanf and iostreams paidojoao-groups@yahoo.com C++ 5 08-04-2004 02:14 PM
fgets() and scanf() peculiarity Eirik C Programming 6 12-12-2003 06:09 AM
help with infinite loops and scanf Rob C Programming 8 07-29-2003 12:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57