我(很明显)很新,并且正在尝试为我的第一个项目构建一个计算器。我想测试我的第一个概念,但是在编译时,我的InterFace结构的最后括号以及int AddUp的第一个括号都出现2059错误。这些似乎是完全随机的错误。如果有帮助,错误是针对行(10,1)和(16,2)的,尽管我怀疑1和2是指记录的类似错误的数量?任何帮助将不胜感激。
1 #include <iostream>
2
3 struct InterFace
4 {
5 char Buttons[4][4]
6 {
7 Buttons[1] = "\u00B1";
8 std::cout << Buttons[1] << std::endl;
9 }
10 };
11
12
13 struct Addition
14 {
15 int AddUp[2]
16 {
17
18 }
19 };
int main()
{
std::cin.get();
}
答案 0 :(得分:1)
您没有正确的核心概念,因此在编写这样的程序之前,应该通读一些C ++教程或课程。
几件事:
char
指单个字节usually an ASCII value if it's referring to text data。因此它不能存储unicode +-符号。相反,您可以将此unicode值存储在std::string buttons[4][4];
中(尽管the full answer is much more complicated)。'a'
指字符a,而"a"
指const char*
。如果不是因为unicode问题,则应该使用单引号。Buttons[1]
,但是按钮是二维数组。元素1还引用数组的 second 元素,这可能不是您想要的。相反,您可以写Buttons[0][0]='a';
这是一个固定的/有效的示例,但我真的建议您先阅读其他教程!
#include <iostream>
#include <string>
struct Interface {
std::string buttons[4][4];
void initialize_interface() {
buttons[0][0] = std::string("\u00B1");
std::cout << buttons[0][0] << std::endl;
}
};
int main() {
Interface my_interface;
my_interface.initialize_interface();
return 0;
}
身为M.M.在评论中指出,以下是一种更范式的方法:
#include #include
struct Interface {
std::string buttons[4][4];
Interface() {
buttons[0][0] = std::string("\u00B1");
std::cout << buttons[0][0] << std::endl;
}
};
int main() {
Interface my_interface;
return 0;
}
Interface::Interface
被称为构造函数,它在初始化时运行。
答案 1 :(得分:1)
由于我无法按照最初的意图构建计算器,因此我选择了另一条路。我用switch完成了一个基本的操作。
#include <iostream>
int main()
{
int r;
int a;
int b;
int result1;
int result2;
int result3;
int result4;
int result5;
std::cout << "Please choose from the available options:" << std::endl << "0. Add" << std::endl << "1. Subtract" << std::endl << "2. Multiply" << std::endl << "3. Divide" << std::endl << "4. Modulo" << std::endl;
std::cin >> r;
switch (r % 5)
{
case 0:
std::cout << "You have chosen to Add, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result1 = a + b;
std::cout << "Your sum is " << result1 << std::endl;
break;
case 1:
std::cout << "You have chosen to Subtract, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result2 = a - b;
std::cout << "Your difference is " << result2 << std::endl;
break;
case 2:
std::cout << "You have chosen to Multiply, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result3 = a * b;
std::cout << "Your product is " << result3 << std::endl;
break;
case 3:
std::cout << "You have chosen to Divide, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result4 = a / b;
std::cout << "Your quotient is " << result4 << std::endl;
break;
case 4:
std::cout << "You have chosen to perform Modulus, please enter two digits" << std::endl;
std::cin >> a;
std::cin >> b;
result5 = a % b;
std::cout << "Your answer is " << result5 << std::endl;
break;
}
std::cin.get();
std::cin.get();
}