PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > confused with cin in C++
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
confused with cin in C++

Réponse
 
LinkBack Outils de la discussion
Vieux 07/04/2008, 17h33   #1
John Ruan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut confused with cin in C++

This is a small program in C++. I want to try to type in real number instead
of integer to see how the program runs.
And I am confused. If I input integer for both variables, the program runs
fine. But if I input a real number for the height (like 68.2), the second
cin doesn't work at all!!

I expected any real number will be truncated and that is my only
expectation. But it seems that there are other problems. Can anybody try it?



#include <iostream>
using namespace std;

int main(void)
{
const double INCHES_PER_METER = 39.37;
const double POUNDS_PER_KG = 2.24;

int height;
int weight;

cout << "METRIC CONVERTER" << endl << endl ;
cout << "Enter your height in inches " ;
cout << "(No fractions, please!) : " ;
cin >> height;

cout << "Enter your weight in pounds" ;
cout << "(No fractions, please!)" ;
cin >> weight;
cout << endl ;

double metric_height = height/INCHES_PER_METER;

double metric_weight = weight/POUNDS_PER_KG;


cout << "Your height is " << metric_height << " meters." << endl;
cout << "Your weight is " << metric_weight << " kilograms." << endl;

return 0;
}


  Réponse avec citation
Vieux 07/04/2008, 18h10   #2
Christopher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: confused with cin in C++

On Apr 7, 11:33 am, "John Ruan" <rds1...@sh163.net> wrote:
> This is a small program in C++. I want to try to type in real number instead
> of integer to see how the program runs.
> And I am confused. If I input integer for both variables, the program runs
> fine. But if I input a real number for the height (like 68.2), the second
> cin doesn't work at all!!
>
> I expected any real number will be truncated and that is my only
> expectation. But it seems that there are other problems. Can anybody try it?
>
> #include <iostream>
> using namespace std;
>
> int main(void)
> {
> const double INCHES_PER_METER = 39.37;
> const double POUNDS_PER_KG = 2.24;
>
> int height;
> int weight;
>
> cout << "METRIC CONVERTER" << endl << endl ;
> cout << "Enter your height in inches " ;
> cout << "(No fractions, please!) : " ;
> cin >> height;
>
> cout << "Enter your weight in pounds" ;
> cout << "(No fractions, please!)" ;
> cin >> weight;
> cout << endl ;
>
> double metric_height = height/INCHES_PER_METER;
>
> double metric_weight = weight/POUNDS_PER_KG;
>
> cout << "Your height is " << metric_height << " meters." << endl;
> cout << "Your weight is " << metric_weight << " kilograms." << endl;
>
> return 0;
>
> }


Your assumption is incorrect. If a stream is unable to convert to the
requested type it sets an error bit. I don't remember which one, but I
believe you can check it with cin.bad(). I would of course refresh my
memory by reading up on streams and stream error handling, but I'll
leave that to you.

The same rules apply to other streams.

That is how you can check whether the user typed in a value of the
type you were expecting and then tell them they aren't following
instructions and to try again.

  Réponse avec citation
Vieux 07/04/2008, 18h14   #3
Bo Persson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: confused with cin in C++

John Ruan wrote:
> This is a small program in C++. I want to try to type in real
> number instead of integer to see how the program runs.
> And I am confused. If I input integer for both variables, the
> program runs fine. But if I input a real number for the height
> (like 68.2), the second cin doesn't work at all!!
>


The input is originally text, which is scanned for digits that can be
part of an integer. The first input reads a 6 and an 8, and gives you
the number 68.

When you try to read a second integer, the first character found is a
period, which is an error for integers. You get no number.



Bo Persson


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10689 seconds with 11 queries