@gliese581d
Учусь

Куда вписать printf?

Прохожу курс cs50, задача лабораторного задания звучит так:

Complete the implementation of population.c, such that it calculates the number of years required for the population to grow from the start size to the end size.

Your program should first prompt the user for a starting population size.
If the user enters a number less than 9 (the minimum allowed population size), the user should be re-prompted to enter a starting population size until they enter a number that is greater than or equal to 9. (If we start with fewer than 9 llamas, the population of llamas will quickly become stagnant!)

Не удается при введении числа меньше 9, вывести на экран, перед возвратом к новому вводу, комментарий о том, что необходимо ввести число равняющееся или больше чем 9.

Каким образом можно это реализовать (printf после while не работает)?
И в целом, хотел реализовать так:

int population = get_int("set a starting population size:")
if (population<9)
printf ("the minimum allowed population size is 9")
return population;

но не работает.

Рабочий код без вывода комментария:

#include <cs50.h>
  #include <stdio.h>

  int get_number(void);

  int main(void)

{
 int i=get_number();
printf("%i\n", i);

}

 int get_number(void)

{
int n;
do
 { n= get_int("set a starting population size: ");
}
while (n<9);
return n;

}
  • Вопрос задан
  • 114 просмотров
Решения вопроса 1
myjcom
@myjcom
int main()
{
  int n = 0;
  while((n = get_int("set a starting population size: ")) < 9)
  {
    printf("...");
  }
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы