使用 cin、if 语句和数组不起作用
Using cin, if statement, and array not working
我正在用 C++ 制作一个 mini-CPU,它使用数组的二进制状态来激活不同的事件。例如,第 67、39 和 23 个值为 1 的数组可能会输出日期。我正在做一个输入测试,其中输入 "a" 会导致第一个实际值为 1。正如您所注意到的,该数组已经以 "a" 开头,但这是 CPU.
的某个部分的指示符
我做了错误报告告诉我做的所有事情,但他们继续发送相同的结果。如果你想要,我可以发送调试。
#include <iostream>
using namespace std;
int main() {
char var a = 1
char myArray = {a, 0, 0, 0, 0, 0, 0, 0, 0};
char var pushregister;
cin >> pushregister;
if (pushregister == a) {
myArray = {a, 1, 0, 0, 0, 0, 0, 0, 0}
};
cout << myArray;
return 0;
}
你的代码看起来不像 C++。
你想要这样的东西吗?
#include <iostream>
#include <cstdint>
#include <cstdlib>
int main()
{
uint8_t a = 1;
uint8_t my_array[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
static const size_t my_array_capacity =
sizeof(my_array) / sizeof(my_array[0]);
my_array[0] = a;
uint8_t push_register;
std::cin >> push_register;
if (push_register == a)
{
my_array[1] = 1;
}
for (size_t i = 0; i < my_array_capacity; ++i)
{
if (i > 0)
{
std::cout << ", ";
}
std::cout << static_cast<unsigned int>(my_array[i]);
}
std::cout << "\n";
return EXIT_SUCCESS;
}
一些差异:
1. 数组不能包含变量,它们包含值。
2. 使用[]
访问数组槽。
3.打印uint8_t
时,强制转换为unsigned int
,避免cout
把变量当成字符。
代码:
string myArray= "a, 0, 0, 0, 0, 0, 0, 0, 0";
int pushregister; cin >> pushregister;
if (pushregister == a) {
myArray = "a, 1, 0, 0, 0, 0, 0, 0, 0"; cout << myArray;
}
else {
cout << "Wrong input" << endl;
}
return 0;
我正在用 C++ 制作一个 mini-CPU,它使用数组的二进制状态来激活不同的事件。例如,第 67、39 和 23 个值为 1 的数组可能会输出日期。我正在做一个输入测试,其中输入 "a" 会导致第一个实际值为 1。正如您所注意到的,该数组已经以 "a" 开头,但这是 CPU.
的某个部分的指示符我做了错误报告告诉我做的所有事情,但他们继续发送相同的结果。如果你想要,我可以发送调试。
#include <iostream>
using namespace std;
int main() {
char var a = 1
char myArray = {a, 0, 0, 0, 0, 0, 0, 0, 0};
char var pushregister;
cin >> pushregister;
if (pushregister == a) {
myArray = {a, 1, 0, 0, 0, 0, 0, 0, 0}
};
cout << myArray;
return 0;
}
你的代码看起来不像 C++。
你想要这样的东西吗?
#include <iostream>
#include <cstdint>
#include <cstdlib>
int main()
{
uint8_t a = 1;
uint8_t my_array[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
static const size_t my_array_capacity =
sizeof(my_array) / sizeof(my_array[0]);
my_array[0] = a;
uint8_t push_register;
std::cin >> push_register;
if (push_register == a)
{
my_array[1] = 1;
}
for (size_t i = 0; i < my_array_capacity; ++i)
{
if (i > 0)
{
std::cout << ", ";
}
std::cout << static_cast<unsigned int>(my_array[i]);
}
std::cout << "\n";
return EXIT_SUCCESS;
}
一些差异:
1. 数组不能包含变量,它们包含值。
2. 使用[]
访问数组槽。
3.打印uint8_t
时,强制转换为unsigned int
,避免cout
把变量当成字符。
代码:
string myArray= "a, 0, 0, 0, 0, 0, 0, 0, 0";
int pushregister; cin >> pushregister;
if (pushregister == a) {
myArray = "a, 1, 0, 0, 0, 0, 0, 0, 0"; cout << myArray;
}
else {
cout << "Wrong input" << endl;
}
return 0;