简体   繁体   English

我如何在一个 if 语句中声明所有数字

[英]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 .更正您在Ifquantitiy上的拼写错误。 Declare and initialize the missing total and price variables.声明并初始化缺失的totalprice变量。 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.

相关问题 如何将字符串中的所有数字一一读入数组(C ++) - How to read all the numbers in the string one by one into array (c++) 我需要 Shift 键的 ASCII 键码。 我在互联网上找到的所有数字都不起作用 - I need ASCII key code for Shift key. All the numbers I've found on the internet doesn't work 我想制作彼此不同但重复的数字 - I want to make numbers that is differnet one another but repeated numbers appeat 如何看待一个数组在C ++中是否具有连续的数字? - How would one see if an array has consecutive numbers in C++? 当我尝试打印所有 USB 设备时,我只能看到一个 USB 字符串 (WinAPI) - I can see only one USB string when I try to print all the USB devices (WinAPI) 一个 if else 语句将不会执行 - One if else statement will not execute 在一行代码中输入2个十六进制数字和一个字符串 - Inputting 2 Hex Numbers and a string in one line of code 我怎样才能在一种情况下重复所有这些代码,而在另一种情况下只重复其中的一部分? - How can I repeat all of this code under one circumstance and only some of it under another? 我想知道我的代码中的错误。 这是打印所有偶数的总和,直到 1 到 N - I want to know the error in my code. This is to print sum of all even numbers till 1 to N 我如何创建单个列表,然后从中创建两个单个列表,其中一个用于偶数,另一个用于奇数? - How i can create a single list , then create two single lists from it one of them for even numbers and another for odd numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM