A
A
Alexander2020-11-17 11:10:48
C++ / C#
Alexander, 2020-11-17 11:10:48

Where to enter printf?

I am taking the cs50 course, the task of the laboratory task is as follows:

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!)

It is not possible when entering a number less than 9 to display, before returning to a new input, a comment that a number equal to or greater than 9 must be entered.

How can I implement this (printf after while does not work)?
And in general, I wanted to implement it like this:

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

but doesn't work.

Working code without comment output:

#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;

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-11-17
@gliese581d

int main()
{
  int n = 0;
  while((n = get_int("set a starting population size: ")) < 9)
  {
    printf("...");
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question