学习 C for CS50 Lab1: Population Growth - Programm runs, but does not calculate anything

问题描述 投票:0回答:0

我正在学习CS50开始学习编程

在进行实验 1 时,我尝试不使用 CS50 库,因为它仅适用于该课程,如果我希望将来能够编程,我认为我应该只学习使用标准可用库进行编程。这就是我正在尝试做我的代码

基本上我想做的是一直要求用户输入一个数字,直到该数字是一个整数,这样如果用户输入另一个不是整数的值,程序将不会输出任何不期望的内容。我没有使用 Cs50 库自带的“get_int”函数,它已经这样做了,所以我可以自己学习如何处理这种情况。

所以我不知道 scanf 函数的“if”语句是否正确检查。任何人都可以向我解释为什么我的代码无法正确运行,或者可以提出另一种解决方案吗?

#include <stdio.h>

int main(void)
{
    int startPop, endPop, numYears,changePop=0;
    // TODO: Prompt for start size
    do
    {
        RepeatStart :
        printf("enter inicial population: ");
       if(scanf("%i",&startPop)!=1)
        {
            goto RepeatStart;
        }

        scanf("%i",&startPop);
    }
    while (startPop<9);

    // TODO: Prompt for end size
    do
    {
    RepeatEnd :
        printf("enter End population: ");
        if(scanf("%i",&startPop)!=1)
        {
            goto RepeatEnd;
        }

       scanf("%i",&endPop);
    }
    while (endPop<9);
    // TODO: Calculate number of years until we reach threshold

    for(numYears=0;changePop<endPop;numYears++)
    {
        changePop=startPop+((startPop/3)-(startPop/4));
    }

    // TODO: Print number of years
    printf("Number of years to reach a population of %i: %i /n",endPop, numYears);
    return 0;
}
c error-handling cs50
© www.soinside.com 2019 - 2024. All rights reserved.