C++ C++;初学者-简单输出
这里我有一些简单的代码作为整数计算器C++ C++;初学者-简单输出,c++,C++,这里我有一些简单的代码作为整数计算器 //Calculator, by Michael Lowry #include <iostream> using namespace std; void main () { int input = 0; int input2 = 0; int answer = 0; int operation = 0; cout << "Enter your first number" <<
//Calculator, by Michael Lowry
#include <iostream>
using namespace std;
void main ()
{
int input = 0;
int input2 = 0;
int answer = 0;
int operation = 0;
cout << "Enter your first number" << endl;
cin >> input;
cout << "Type 1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division" << endl;
cin >> operation;
cout << "Enter your second number" << endl;
cin >> input2;
if (operation = 1)
{
input + input2 == answer;
}
if (operation = 2)
{
input - input2 == answer;
}
if (operation = 3)
{
input * input2 == answer;
}
if (operation = 4)
{
input / input2 == answer;
}
cout << "Your answer is " <<
cout << answer << endl;
system ("PAUSE");
}
//计算器,作者:迈克尔·洛瑞
#包括
使用名称空间std;
空干管()
{
int输入=0;
int input2=0;
int-answer=0;
整数运算=0;
不能输入;
cout操作;
cout输入2;
如果(操作=1)
{
输入+输入2==答案;
}
如果(操作=2)
{
输入-输入2==答案;
}
如果(操作=3)
{
输入*输入2==答案;
}
如果(操作=4)
{
输入/输入2==答案;
}
cout有几个错误:这是为操作
变量指定一个值,而不是将其与以下内容进行比较:
if (operation = 1)
应该是这样
if(operation == 1)
answer = input + input2;
此外,这不会将input+input2
的结果分配给答案,而是进行未使用的比较评估
input + input2 == answer;
应该是这样
if(operation == 1)
answer = input + input2;
您应该相应地更改代码。最后:
cout << "Your answer is " <<
cout << answer << endl;
另外:main()
应该返回int
因此,您的代码应该如下所示:
int main () {
int input = 0;
int input2 = 0;
int answer = 0;
int operation = 0;
cout << "Enter your first number" << endl;
cin >> input;
cout << "Type 1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division" << endl;
cin >> operation;
cout << "Enter your second number" << endl;
cin >> input2;
if (operation == 1) {
answer = input + input2;
}
if (operation == 2) {
answer = input - input2;
}
if (operation == 3) {
answer = input * input2;
}
if (operation == 4) {
answer = input / input2;
}
cout << "Your answer is " << answer << endl;
system ("PAUSE");
}
int main(){
int输入=0;
int input2=0;
int-answer=0;
整数运算=0;
不能输入;
cout操作;
cout输入2;
如果(操作==1){
答案=输入+输入2;
}
如果(操作==2){
答案=输入-输入2;
}
如果(操作==3){
答案=输入*输入2;
}
如果(操作==4){
答案=输入/输入2;
}
cout有几个错误:这是为操作
变量指定一个值,而不是将其与以下内容进行比较:
if (operation = 1)
应该是这样
if(operation == 1)
answer = input + input2;
此外,这不会将input+input2
的结果分配给答案,而是进行未使用的比较评估
input + input2 == answer;
应该是这样
if(operation == 1)
answer = input + input2;
您应该相应地更改代码。最后:
cout << "Your answer is " <<
cout << answer << endl;
另外:main()
应该返回int
因此,您的代码应该如下所示:
int main () {
int input = 0;
int input2 = 0;
int answer = 0;
int operation = 0;
cout << "Enter your first number" << endl;
cin >> input;
cout << "Type 1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division" << endl;
cin >> operation;
cout << "Enter your second number" << endl;
cin >> input2;
if (operation == 1) {
answer = input + input2;
}
if (operation == 2) {
answer = input - input2;
}
if (operation == 3) {
answer = input * input2;
}
if (operation == 4) {
answer = input / input2;
}
cout << "Your answer is " << answer << endl;
system ("PAUSE");
}
int main(){
int输入=0;
int input2=0;
int-answer=0;
整数运算=0;
不能输入;
cout操作;
cout输入2;
如果(操作==1){
答案=输入+输入2;
}
如果(操作==2){
答案=输入-输入2;
}
如果(操作==3){
答案=输入*输入2;
}
如果(操作==4){
答案=输入/输入2;
}
你的输出出了问题。你应该
cout << "Your answer is " << answer << endl;
你的输出出错了,你应该
cout << "Your answer is " << answer << endl;
第一:使用
if (operation == 1)
而不是
cout << "Your answer is " <<
cout << answer << endl;
if (operation = 1)
input + input2 == answer;
因为=
表示相等,=
表示赋值
第二:
answer = input1 + input2;
而不是
cout << "Your answer is " <<
cout << answer << endl;
if (operation = 1)
input + input2 == answer;
在所有if
语句中执行此操作
第三:使用
cout << "Your answer is " << answer << endl;
cout首先:使用
if (operation == 1)
而不是
cout << "Your answer is " <<
cout << answer << endl;
if (operation = 1)
input + input2 == answer;
因为=
表示相等,=
表示赋值
第二:
answer = input1 + input2;
而不是
cout << "Your answer is " <<
cout << answer << endl;
if (operation = 1)
input + input2 == answer;
在所有if
语句中执行此操作
第三:使用
cout << "Your answer is " << answer << endl;
不能先出错
如果要将值分配给变量应答
,应执行以下操作:
answer = input1 (required operator here) input2;
在您的代码中,这样的构造:
input - input2 == answer;
有两个方面是错误的:
如果要分配值来回答,请使用分配=
不比较=
运算符
赋值从右向左,因此所需变量应位于左侧
第二个错误
在if(val=yourConstant)
行中,您犯了一个非常常见的错误,即在if语句内部赋值。许多语言禁止这样做,因为在不进行调试或测试的情况下很难检测到。if语句内部的代码只有在yourConstant
大于0时才会执行。请改用if(val==yourConstant)
或如果(yourConstant==val)
第一个错误
如果要将值分配给变量应答
,应执行以下操作:
answer = input1 (required operator here) input2;
在您的代码中,这样的构造:
input - input2 == answer;
有两个方面是错误的:
如果要分配值来回答,请使用分配=
不比较=
运算符
赋值从右向左,因此所需变量应位于左侧
第二个错误
在if(val=yourConstant)
行中,您犯了一个非常常见的错误,即在if语句内部赋值。许多语言禁止这样做,因为在不进行调试或测试的情况下很难检测到。if语句内部的代码只有在yourConstant
大于0时才会执行。请改用if(val==yourConstant)
或如果(yourConstant==val)
那么,不要:
if (operation = 1)
做:
同时将输出更改为:
cout
if (operation = 1)
做:
同时将输出更改为:
cout-enable compiler warnings.enable compiler warnings.if值是常量,建议使用if(yourcontainst==val)
编译器将帮助您轻松捕获错误如果值是常量,建议使用if(yourcontainst==val)
编译器将帮助您更轻松地捕获错误