Answer the question
In order to leave comments, you need to log in
Why does the first cycle run again?
Please tell me what could be the problem, logically everything seems to be correct, but the program does not work as it should. Instead of processing block by block, it repeats the loop several times, and the condition <9 does not work, when you enter the number 9, instead of moving on to the next command, it repeats it again.
~/ $ ./population
set starting population size: 9 (должна была перейти к след. команде)
number should be equal or greater than 9: 9
set ending population size: 9 (должна была перейти к след. команде)
number should be equal or greater than starting population size: 9
set starting population size: 9
number should be equal or greater than 9: 9 - конец
#include <cs50.h>
#include <stdio.h>
int start_int();
int end_int();
int main (void)
{
int i = start_int();
int j = end_int();
}
//prompt the user for a starting population size
int start_int(void)
{
int y;
do
{
y = get_int("set starting population size: ");
}
while ((y=get_int("number should be equal or greater than 9: "))<9);
return y;
}
//prompt the user for an ending population size
int end_int(void)
{
int x;
do
{
x = get_int("set ending population size: ");
}
while ((x=get_int("number should be equal or greater than starting population size: "))<start_int());
return x;
}
while ((x=get_int("number should be equal or greater than starting population size: "))<start_int());
and while ((у=get_int("number should ...;
#include <cs50.h>
#include <stdio.h>
int start_int();
int end_int();
int main (void)
{
int i = start_int();
int j = end_int();
}
//prompt the user for a starting population size
int start_int(void)
{
int y;
do
{
y = get_int("set starting population size: ");
}
while (y<9);
return y;
}
//prompt the user for an ending population size
int end_int(void)
{
int x;
do
{
x = get_int("set ending population size: ");
}
while (x<start_int());
return x;
}
~/ $ ./population
set starting population size: 9
set ending population size: 9
set starting population size: 9(не должно быть)
Answer the question
In order to leave comments, you need to log in
If you remove this part, then the program works properly, except for the re-execution of the first do while loop.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question