不受控制的循环和函数 C++跳过
Uncontrolled looping and function Skipping in C++?
我正在尝试从键盘读取两个用户输入的数字到发现板上。我有代码用于读取第一个数字,但由于某种原因,当它点击相同的键盘((;函数第二次它似乎没有调用函数以允许输入,而是跳过扫描并打印其下方的行,如果您按下按钮重新开始,程序拾取的地方是随机的,任何可能导致这种情况的想法。我正在在线 Ide 上编译。下面是代码。
#include <iostream>
#include "mbed.h"
DigitalIn columns[3] = {PB_6, PB_7, PD_0}; // Columns for digital input
DigitalOut rows[4] = {PA_5, PA_1, PA_2, PA_3}; // rows for digital output
DigitalIn startButton(USER_BUTTON);
DigitalOut led1(LED1); // reference LED
int numpad[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {-2, 0, -1}}; // keypad
int Total();
int Keypad();
int c = 0;
int Read;
int Num1 = 0;
int SelectOp();
int Oper;
int main() {
while (1) {
if (startButton == 1) {
printf("%snrInput First Numbernr");
wait(.5);
Keypad();
int First = Num1;
Num1 = 0;
printf("%snr Your first number is ");
printf("%i", First);
printf("%snr Input your second numbernr");
wait(.5);
Keypad(); // this seems to be getting skipped
int Second = Num1;
Num1 = 0;
printf("%snr Your Second number is ");
printf("%i", Second);
printf("%snrSelect Operator: 1(+), 2(-), 3(*), 4(/)");
Keypad();
Oper = Num1;
}
}
}
int Keypad() {
columns[0].mode(PullUp);
columns[1].mode(PullUp);
columns[2].mode(PullUp);
while (1) {
if (Read == -1) {
return Num1;
}
for (int i = 0; i < 4; i++) {
rows[0] = 1;
rows[1] = 1;
rows[2] = 1;
rows[3] = 1;
rows[i] = 0;
wait(0.01);
for (int j = 0; j < 3; j++) {
if (columns[j] == 0) {
Read = numpad[i][j];
Total();
c++;
if (c == 5) {
c = 0;
}
wait(0.005);
while (columns[j] == 0)
;
}
}
}
}
}
int Total() {
if (Read >= 0) {
Num1 *= 10;
Num1 += Read;
printf("%inr", Num1);
} else {
return Num1;
}
return Num1;
}
当 Read
在通过 Keypad(( 的第一个循环中设置为 -1 时,当您再次输入 Keypad()
时它仍然是 -1,从而立即返回。
使用一些空间来呼应@Scheff根据变量的预期生存期确定变量范围的重要性,从而尽可能最小化全局变量。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗