Explicación
Publicado por Estudiante (1 intervención) el 03/04/2015 04:43:40
Necesito que por favor me expliquen cada linea después de "Declarar Variables". necesito hacerlo con los codios: Cout y Cin.
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
#include <iostream>
#include <ctype.h> // isdigit
#include <stdlib.h> // atoi
#include <string.h> // strlen
#include <stdio.h> // printf, fgets, stdin, BUFSIZ
using namespace std;
int main()
{
//Declarar Variables
int number;
unsigned n;
char buffer[BUFSIZ];
bool found_nondigit, valid;
//Proceso
valid = false;
while (!valid) {
printf ( "Enter an integer: " );
if (fgets (buffer, sizeof buffer, stdin) != NULL){
buffer[strlen(buffer)-1] = \0 ;
found_nondigit = false;
if (strlen(buffer) == 0) // comprueba si el usuario ha pulsado "Intro" sin introducir una entrada
found_nondigit = true;
for (n=0; n<strlen(buffer); n++)
if (!isdigit(buffer[n]))
found_nondigit = true;
if (!found_nondigit){
number = atoi(buffer); // convierte la entrada en un entero cuando se ha validado
printf ("%d\n", number);
valid = true;}
else
printf ( "Error: Invalid input\n" );
}
}
return 0;
}
Valora esta pregunta


0