Смысл кода - C (СИ)
Формулировка задачи:
Имеется следующий код, мы вводим числовое значение и если оно некорректное (присутствуют буквы и т.п.) то программа его не принимает, требует снова ввести входное значение.
В частности только этот кусок непонятен:
Буду очень благодарен за подробное разъяснение ( желательно как можно более доступно ясно). Спасибо!
bool is_valid_formatting(int num_args_read, int num_args_needed){
char new_line = '\n';
bool is_valid = true; //assume the format is correct
if(num_args_read != num_args_needed){ //didn't fill in all the format specifiers
is_valid = false;//format is not correct
}
do{
scanf("%c", &new_line); //read the next character from the standard input
if(!isspace(new_line)){ //if it isn't white space
is_valid = false; //extra stuff there so format isn't valid
}
}while(new_line != '\n'); //keep reading characters until you reach the new line character
return is_valid;
}
double get_valid_payment(){
double num;
int num_args_read;
do{
printf("Enter how much money you will be putting towards loans/retirement each month: ");
num_args_read = scanf("%lf", &num);
}while(!(is_valid_formatting(num_args_read, 1)));
return num;
}
int main(){
payment = get_valid_payment;do{
scanf("%c", &new_line); //read the next character from the standard input
if(!isspace(new_line)){ //if it isn't white space
is_valid = false; //extra stuff there so format isn't valid
}
}while(new_line != '\n'); //keep reading characters until you reach the new line character
return is_valid;Решение задачи: «Смысл кода»
textual
Листинг программы
scanf("%lf", &num);
Объяснение кода листинга программы
- Вводится число с плавающей точкой в переменную
numс помощью функции scanf()