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
specifier | Output | Example |
c | Character | a |
d or i | Signed decimal integer | 392 |
e | Scientific notation (mantise/exponent) using e character | 3.9265e+2 |
E | Scientific notation (mantise/exponent) using E character | 3.9265E+2 |
f | Decimal floating point | 392.65 |
g | Use the shorter of %e or %f | 392.65 |
G | Use the shorter of %E or %f | 392.65 |
o | Signed octal | 610 |
s | String of characters | sample |
u | Unsigned decimal integer | 7235 |
x | Unsigned hexadecimal integer | 7fa |
X | Unsigned hexadecimal integer (capital letters) | 7FA |
p | Pointer address | B800:0000 |
n | Nothing 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
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
using namespace std;
int main() {
char name[80];
int age=0;
cout<<"Enter your name ?"<>name;
cout<<"Enter your age ?"<>age;
cout<<"Name : "<>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
#include //for numeric_limits
using namespace std;
int main() {
int input;
cout<<"Entre un entier : \n"<>input))
{
cin.clear();
cin.ignore(std::numeric_limits::max(),'\n');
cout<<"Entre un entier : \n"<