你能帮我解决这段代码中的错误吗?
Can you help me to solve errors in this code?
我最近开始学习C++,我正在尝试创建一个带有一些操作的“计算器”。
但我被困在字符串 26 (cin >> choose;) 中,这似乎没有反应。为什么?
//CALCULATOR
#include <iostream>
using namespace std;
int main()
{
int start;
int choose;
int first;
int second;
int unic;
int result;
cout << "CALCULATOR (2 values)" << endl;
cout << "Click a botton to continue: " << endl;
cin >> start;
cout << "Write: " << endl;
cout << "- '1' for sum" << endl;
cout << "- '2' for subtraction" << endl;
cout << "- '3' for moltiplication" << endl;
cout << "- '4' for power of 2" << endl;
cout << "Your answer: " << endl;
cin >> choose;
cout << "________________________" << endl;
{
if (choose=1, 2, 3)
cout << "Insert the first value: " << endl;
cin >> first;
cout << "Insert the second value: " << endl;
cin >> second;
{
if (choose=1)
result = first + second;
}
{
if (choose=2)
result = first + second;
}
{
if (choose=3)
result = first * second;
}
{
if (choose=4)
cout << "Insert the value: " << endl;
cin >> unic;
result = unic * unic;
}
}
cout << "Your result is: " << result;
}
它没有给我任何错误,但它继续执行我写的所有“cout”操作,没有给我用“cin”写我的值的可能性。
您的代码中存在几个问题,
- 在 C++ 中,= 代表赋值来检查值是否相等,您必须使用 ==
if (choose = 1, 2, 3){
...
// this doesn't work in C/C++
// change it into
if (choose == 1 || choose == 2 || choose == 3)
当条件(if/else 或循环 for/while)下有不止一行代码时,您需要将它们显式地阻止在大括号内。因此,如果首先将 if 块放入 this
if (choose == 1 || choose == 2 || choose == 3){
cout << "Insert the first value: " << endl;
cin >> first;
cout << "Insert the second value: " << endl;
cin >> second;
...
嵌套 if 条件也是如此。
也没有理由在开始时接受输入。
如果您修复了所有错误,您应该会得到这样的代码 ->
//CALCULATOR
#include <iostream>
using namespace std;
int main()
{
int start;
int choose;
int first;
int second;
int unic;
int result;
cout << "CALCULATOR (2 values)" << endl;
cout << "Click a botton to continue: " << endl;
// cin >> start;
cout << "Write: " << endl;
cout << "- '1' for sum" << endl;
cout << "- '2' for subtraction" << endl;
cout << "- '3' for moltiplication" << endl;
cout << "- '4' for power of 2" << endl;
cout << "Your answer: " << endl;
cin >> choose;
cout << "________________________" << endl;
if (choose == 1 || choose == 2 || choose == 3){
cout << "Insert the first value: " << endl;
cin >> first;
cout << "Insert the second value: " << endl;
cin >> second;
if (choose == 1)
result = first + second;
if (choose == 2)
result = first + second;
if (choose == 3)
result = first * second;
}
if (choose == 4){
cout << "Insert the value: " << endl;
cin >> unic;
result = unic * unic;
}
cout << "Your result is: " << result << endl;
}
脚注:请使用给定的代码作为参考,并尝试仔细理解基础知识。你这样做很重要。
我最近开始学习C++,我正在尝试创建一个带有一些操作的“计算器”。 但我被困在字符串 26 (cin >> choose;) 中,这似乎没有反应。为什么?
//CALCULATOR
#include <iostream>
using namespace std;
int main()
{
int start;
int choose;
int first;
int second;
int unic;
int result;
cout << "CALCULATOR (2 values)" << endl;
cout << "Click a botton to continue: " << endl;
cin >> start;
cout << "Write: " << endl;
cout << "- '1' for sum" << endl;
cout << "- '2' for subtraction" << endl;
cout << "- '3' for moltiplication" << endl;
cout << "- '4' for power of 2" << endl;
cout << "Your answer: " << endl;
cin >> choose;
cout << "________________________" << endl;
{
if (choose=1, 2, 3)
cout << "Insert the first value: " << endl;
cin >> first;
cout << "Insert the second value: " << endl;
cin >> second;
{
if (choose=1)
result = first + second;
}
{
if (choose=2)
result = first + second;
}
{
if (choose=3)
result = first * second;
}
{
if (choose=4)
cout << "Insert the value: " << endl;
cin >> unic;
result = unic * unic;
}
}
cout << "Your result is: " << result;
}
它没有给我任何错误,但它继续执行我写的所有“cout”操作,没有给我用“cin”写我的值的可能性。
您的代码中存在几个问题,
- 在 C++ 中,= 代表赋值来检查值是否相等,您必须使用 ==
if (choose = 1, 2, 3){
...
// this doesn't work in C/C++
// change it into
if (choose == 1 || choose == 2 || choose == 3)
当条件(if/else 或循环 for/while)下有不止一行代码时,您需要将它们显式地阻止在大括号内。因此,如果首先将 if 块放入 this
if (choose == 1 || choose == 2 || choose == 3){ cout << "Insert the first value: " << endl; cin >> first; cout << "Insert the second value: " << endl; cin >> second; ...
嵌套 if 条件也是如此。
也没有理由在开始时接受输入。
如果您修复了所有错误,您应该会得到这样的代码 ->
//CALCULATOR
#include <iostream>
using namespace std;
int main()
{
int start;
int choose;
int first;
int second;
int unic;
int result;
cout << "CALCULATOR (2 values)" << endl;
cout << "Click a botton to continue: " << endl;
// cin >> start;
cout << "Write: " << endl;
cout << "- '1' for sum" << endl;
cout << "- '2' for subtraction" << endl;
cout << "- '3' for moltiplication" << endl;
cout << "- '4' for power of 2" << endl;
cout << "Your answer: " << endl;
cin >> choose;
cout << "________________________" << endl;
if (choose == 1 || choose == 2 || choose == 3){
cout << "Insert the first value: " << endl;
cin >> first;
cout << "Insert the second value: " << endl;
cin >> second;
if (choose == 1)
result = first + second;
if (choose == 2)
result = first + second;
if (choose == 3)
result = first * second;
}
if (choose == 4){
cout << "Insert the value: " << endl;
cin >> unic;
result = unic * unic;
}
cout << "Your result is: " << result << endl;
}
脚注:请使用给定的代码作为参考,并尝试仔细理解基础知识。你这样做很重要。