[英]How could I declare all numbers in one if statement I've try declaring all numbers by else if one by one but it seems the code has logical error
#include <iostream>
using namespace std;
int main ()
{
int quantity;
again:
cout<<"Enter the quantity: ";
cin>>quantity;
If(quantitiy == '1' )
{
total = quantity*price
}
else
{
goto again;
}
return 0;
}
How could I declare all the numbers in one if statement?如何在一个 if 语句中声明所有数字?
Pretty much everything in your code is wrong and needs fixing.您的代码中几乎所有内容都是错误的,需要修复。 Use a
do..while
loop instead of goto
.使用
do..while
循环而不是goto
。 Correct your typos on If
and quantitiy
.更正您在
If
和quantitiy
上的拼写错误。 Declare and initialize the missing total
and price
variables.声明并初始化缺失的
total
和price
变量。 Add the missing ;
添加缺少的
;
on the total
assignment.关于
total
任务。 And '1'
and 1
are two completely different types and values.而
'1'
和1
是两个完全不同的类型和值。
Try something more like this instead:尝试更多类似的东西:
#include <iostream>
using namespace std;
int main ()
{
int quantity;
double price = ...; // for you to decide, unless you ask the user...
double total = 0;
do {
cout << "Enter the quantity: ";
cin >> quantity;
}
while (quantity != 1);
total = quantity*price;
return 0;
}
Of course, it really doesn't make sense to prompt the user for a quantity and then only accept 1
.当然,提示用户输入数量然后只接受
1
确实没有意义。 You probably want something more like this:你可能想要更像这样的东西:
#include <iostream>
using namespace std;
int main ()
{
int quantity;
double price = ...; // for you to decide, unless you ask the user...
double total = 0;
do {
cout << "Enter the quantity (0 to stop): ";
cin >> quantity;
if (quantity == 0) break;
total += (quantity * price);
}
while (true);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.