c - 无法在 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");
}
}
解决方案
由于您已经声明cycle
为全局变量,因此计算周期数的最佳方法是做cycle++
.
推荐阅读
- c - 读取没有功能的字符串
- c - 为什么我要添加 rax 和 rdx?
- android - 列表视图中的 csv 和复选框将选中的复选框代码保存在 ArrayAdapter 中
- vue.js - vue-multiselect 两个控件一个源没有项目重叠
- reactjs - 使用 React-Router,你可以设置一个“外部”组件在每条路由上渲染吗?
- javascript - 如何将字符串转换为数组到新的另一个数组?
- selenium - 从属性文件传递黄瓜特征文件数据表中的值
- angular - ngFor 带索引
- oracle - kafka 流与 oracle 数据库与开源
- python - Pylint 和 Fabric 上下文管理器