C++
Flux Entrée/sorties
En C++, le détail des opérations d'écriture sur l'écran ou dans un fichier, ou celui de la lecture à partir du clavier ou dans un fichier sont encapsulés dans les flux. Les flux prennent en charge le transport puis l'affichage ou l'écriture des données sur le disque ou l'écran de l'ordinateur.
Stdio
specifierOutputExample
cCharactera
d or iSigned decimal integer392
eScientific notation (mantise/exponent) using e character3.9265e+2
EScientific notation (mantise/exponent) using E character3.9265E+2
fDecimal floating point392.65
gUse the shorter of %e or %f392.65
GUse the shorter of %E or %f392.65
oSigned octal610
sString of characterssample
uUnsigned decimal integer7235
xUnsigned hexadecimal integer7fa
XUnsigned hexadecimal integer (capital letters)7FA
pPointer addressB800:0000
nNothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.
%A % followed by another % character will write % to stdout.
#include <stdio.h> int main() { char name[80]; int age=0; printf("Enter your name ?\n"); scanf("%s",name); printf("Enter your age ?\n"); scanf("%d",&age); printf("Name : %s ,age : %d",name,age); return 0; }
Iostream
#include <iostream> using namespace std; int main() { char name[80]; int age=0; cout<<"Enter your name ?"<<endl; cin >>name; cout<<"Enter your age ?"<<endl; cin >>age; cout<<"Name : "<<name<<endl; cout<<"Age : "<<age<<endl; cin >>age; return 0; }
Si nous avions un "input" qui ne correspondait pas à ce que nous attendions :
  • std::cin.clear(), pour sortir le flux de l'état d'échec.
  • std::cin.ignore(...), supprimer le mauvais input de notre flux.
  • et recommencer
#include <iostream> #include<limits> //for numeric_limits using namespace std; int main() { int input; cout<<"Entre un entier : \n"<<endl; while(!(cin >>input)) { cin.clear(); cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); cout<<"Entre un entier : \n"<<endl; } }
Imprimer
Notez cette page

S'il vous plait, prenez un moment pour remplir ce formulaire pour nous aider à mieux vous servir.

12345

    :: Ajouter aux favoris :: Contact