首页 > 解决方案 > 无法在 ac 函数中返回正确的变量

问题描述

我希望这个程序跟踪用户输入值的周期数,程序运行良好,但函数cycler()没有返回适当的值。我会感谢任何在这里帮助我的人。

#include <stdio.h>
#include <stdlib.h>

int cycle=0;

int main()
{
    startProgram(0);//If status is 1, the program will exit
    return 0;
}

void startProgram(int status) {
    if (status == 0) {

        printf("Enter a value\n");
        int input;
        scanf("%d",&input);
        printf("input is: %d\n",input);

        /*
           Here is where i need help!!!!!!!!!!
           When the cycler() is called, i want it to pass in the value of current cycle,
           The value of cycle has first been initialized to 0  
        */
        int cycle = cycler(cycle); 

        printf("Cycle Number : %d\n",cycle);

        resetProgram(input);

    } else {
        printf("Exiting");
    }
}

int cycler(int x){
    int ret = x++;
    return ret;
}

void resetProgram(int status){
    if ((status > 0) && (status < 12)) {
        startProgram(0);
    } else {
        printf("\nExit\n");
    }
}

标签: c

解决方案


由于您已经声明cycle为全局变量,因此计算周期数的最佳方法是做cycle++.


推荐阅读