A
A
Alexander2020-11-20 09:43:23
C++ / C#
Alexander, 2020-11-20 09:43:23

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 - конец


source:

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

}


As I found out, partly there was a problem in the repeated call of the get_int function in
while ((x=get_int("number should be equal or greater than starting population size: "))<start_int());
and while ((у=get_int("number should ...;

If you remove this part, then the program works properly, except for the re-execution of the first do while loop. However, in this form, it does not display a comment in case of an incorrectly entered number, if printf is put before return, then in this case, the comment is always displayed, regardless of the conditions. If after the return command, then it is never displayed.

#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

1 answer(s)
R
res2001, 2020-11-20
@gliese581d

If you remove this part, then the program works properly, except for the re-execution of the first do while loop.

You yourself are re-calling start_int() in the while clause, hence the re-call.
You need to pass to end_int() the saved result of the start_int execution. And in while compare with it without calling start_int again.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question