让用户输入输入,直到他输入X.

时间:2013-12-19 21:10:42

标签: c++

我是编程的初学者,几天前刚开始上课..还在学习这个概念。所以..嗯 我正在用c ++编写一个代码,它从用户那里获取整数,然后按“x”停止。然后程序将打印正数,负数和零数。 但我在这里有一点问题......每当用户输入“x”时,程序进入无限循环...... 我试图删除“ZEROS”部分,只是为正面和负面做了计数器,它运作良好。但我想算零......

所以我需要的是简短的。让用户给出包括0的数字,直到他给出字符X. 这是我的代码..我知道事情并不那么容易..但我是新手:D

#include <iostream>

using namespace std;

int main() {
    int input, neg = 0, pos = 0, zer = 0;
    char z;

    do {
        cout << "Input another positive/negative number or 'x' to stop\n";
        cin >> input;
        cin.ignore();

        if (input > 0){
            pos++;
        } else if (input == 0){
            zer++;
        } else if(input < 0){
            neg++;
        }
    } while (z!='x');

    cout << "You entered " << pos << " positive numbers.\n";
    cout << "You entered " << neg << " negative numbers.\n";
    cout << "You entered " << zer << "Zeros.";

    return 0;
}

5 个答案:

答案 0 :(得分:2)

到目前为止,在用户输入其他内容之前获取数字的最简单方法是:

int input = 0;
cout << "Input a positive/negative number or 'x' to stop\n";
while(cin >> input) {
    //they entered a number, do stuff    
    if (input > 0)
        pos++;
    else if (input == 0)
        zer++;
    else if (input < 0)
        neg++;

    cout << "Input another positive/negative number or 'x' to stop\n";
}
//cin failed to read a number, probably because they entered a letter
//if they failed to enter a number, we need to clear the fail flag before we can use cin again
cin.setstate(cin.rdstate()&~std::ios_base::failbit); 

cout << "You entered " << pos << " positive numbers.\n";
cout << "You entered " << neg << " negative numbers.\n";
cout << "You entered " << zer << "Zeros.";

在你使用C ++非常先进之前,我不建议任何更复杂的东西。解析输入非常难以正确获取,许多有经验的人都错了。

答案 1 :(得分:1)

为了正确处理输入错误并限制它以便只有小写x会破坏你的循环,你需要做很多错误检查:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    int neg = 0;
    int pos = 0;
    int zer = 0;
    std::string line;
    while (std::cin >> line)
    {
        if (line == "x")
        {
            break;
        }

        std::istringstream iss(line); // convert to a stringstream
        int val = 0;
        if (!(iss >> val)) // if we can load an int, do it, otherwise show and error message
        {
            std::cout << "Please enter a valid number!" << std::endl;
            continue;
        }

        if (val > 0)
        {
            pos++;
        }
        else if (val < 0)
        {
            neg++;
        }
        else
        {
            zer++;
        }
    }

    std::cout << "You entered " << pos << " positive numbers.\n"
              << "You entered " << neg << " negative numbers.\n"
              << "You entered " << zer << " zeros." << std::endl;
    return 0;
}

答案 2 :(得分:0)

问题是int类型的对象可能无法从流中读取符号'x'的符号。它期望输入流中的数字。因此,当在输入流中遇到不能在数字中的符号时,会出现错误。流将具有错误状态。如果您将一次又一次地尝试读取数字,则流将不会由于其状态而提供任何内容,并且下一个符号例如是非数字的事实。

因此将变量输入与'x'进行比较是没有意义的。

我会按以下方式重写你的循环

while ( true )
{
    int input;

    cout << "Input another positive/negative number or 'x' to stop: ";
    if ( !( cin >> input ) ) break;

    if (input > 0)
    {
        pos++;
    } else if (input == 0)
    {
        zer++;
    } else
    {
        neg++;
    }
}

答案 3 :(得分:0)

检查出来

#include <iostream>
#include<string>
using std::string;
using std::getline;

using namespace std;

int main() 
{
string input;
int neg = 0, pos = 0, zer = 0;
char z;
input = "";
int iVal = 0;



do
{
    cout << "Input another positive/negative number or 'x' to stop\n";
    getline(cin, input);

    iVal = atoi(input.c_str());
    if (input != "x" && input !="X")
    {
        if (iVal > 0)
        {
            pos++;
        } else if (iVal  == 0)
        {
            zer++;
        } else if(iVal  < 0)
        {
            neg++;
        }
    }
} while (input != "x" && input != "X");

cout << "You entered " << pos << " positive numbers.\n";
cout << "You entered " << neg << " negative numbers.\n";
cout << "You entered " << zer << " zeros.\n";

return 0;
system("pause");

}

答案 4 :(得分:-1)

由于一些原因,IT进入循环

1)您将输入声明为整数,为此,ypu必须将其声明为char数据类型才能对其进行验证。 2)你没有x的if条件,如果(input =='x')

相关问题