在C ++中不允许相同的输入

时间:2018-06-03 14:15:53

标签: c++ strcmp

我试图弄清楚如何用户阻止相同的输入两次或更多次。例如,程序要求用户输入3个输入而不是相同的输入。但是现在我的程序仍然继续选择相同的输入。如果用户输入B2 3次,则选择3次。输出将是You have chosen these lots: B2 B2 B2,这是不可能的。

我的输入是数组btw。 code[3]

我希望它能够先读取相同的输入并说它已经被选中,选择另一个作为输入。

我更喜欢使用strcmp

#include <iostream>
#include <iomanip>
#include <string.h>
#include <fstream>
#include <conio.h>

char code[3][10];

for(int a=0; a<3; a++)
{
    do
    {
        cout << "Please enter the lot you are interested in (A1-A7 / B1-B7): ";
        cin >> ws;
        cin.getline(code[a], 10);

        if((strcmp(code[a], "A4") == 0) || (strcmp(code[a], "A6") == 0) || (strcmp(code[a], "B1") == 0)))
        {
            cout << "ERROR: Sorry! The house you chose has already been booked! \n\n"; // this are for booked lots already from the system
        }
        else if((strcmp(code[a], "A1") == 0) || (strcmp(code[a], "A2") == 0) || (strcmp(code[a], "A3") == 0) ....
        {
            cout << "SUCCESS: You have chosen the LOT " << code[a] << endl << endl;
        }
        else
        {
            cout << "ERROR: Sorry! The lot you entered is unavailable!" << endl << endl;
            // i added strcpy(code[a], "A4"); to trick the system, and it works out. 
        }


    }while((strcmp(code[a], "A4") == 0) || (strcmp(code[a], "A6") == 0) || (strcmp(code[a], "B1") == 0));
}

3 个答案:

答案 0 :(得分:1)

如果你必须稍微改变一下这个问题,你会发现根本不需要字符串或更高级别的数据结构。

输入都是一个字符和一个数字。如果您将其读作一个字符和一个数字,则可以使用2D数组

bool inuse[2][7] = {};

存储任何批次的状态。 inuse[character][digit-1]告诉您之前是否已查看并设置了该特定字符和数字组合。

这是通常的输入验证,以确保用户输入可用的内容。

char group;
unsigned int index;

cin >> group >> index; // get one character and one number
if (cin) // Above reads succeed and data is good.
{
    if ((group == 'A' or group == 'B') and (index > 0 and index <= 7))
    { // input ranges are valid
        int groupnum = group -'A'; // for any sane character encoding 
                                   // 'A' = 0, 'B' = 1, 'C' = 2 ...
        unsigned int number = index - 1; // arrays are origin 0
        if (not inuse[groupnum][number])
        {
            cout << "Lot " << group << index << "selected\n";
            inuse[groupnum][number] = true; // mark as selected
        }
        else
        {
            cout << "Lot " << group << index << "already selected.\n";
        }
    }
    else
    {
        cout << "Invalid input.\n";
    }
}
else
{ 
    cout << "Invalid input.\n";
    cin.clear();
    // bit of a blind spot here if some fool closes the console. Check for eof
    // and exit the program if this is a concern.
}
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard remainder of line

答案 1 :(得分:0)

检查第二个输入是否等于第一个输入:

if (strcmp(code[1], code[0]) == 0)
{
    cout << "This input appeared earlier. Please enter another input\n";
}

要检查第三个输入是否等于第一个或第二个输入,完全按字面意思:

if (strcmp(code[2], code[0]) == 0 || strcmp(code[2], code[1]) == 0)
{
    cout << "This input appeared earlier. Please enter another input\n";
}

但是,如果你想将它扩展到3个以上的输入,你应该保留所有早期输入的列表(如Biffen所建议的那样):

std::set<std::string> earlierInputs;
...
cin.getline(code[a], 10);
auto result = earlierInputs.insert(code[a]);

if (!result.second)
{
    cout << "This input appeared earlier. Please enter another input\n";
}

此代码(需要使用C ++标准库)将新输入插入到所有早期输入的set中。 result是一个数据结构,其second元素告诉insert操作是否成功(即新输入是否已经出现在set中)。

这个稍微模糊的代码最终是为了获得最佳性能;还有其他一些方法可以实现这一点 - 请参阅this related answered question

答案 2 :(得分:-1)

试试这个:

int numValid = 0;
string in1, in2, in3;
while(numValid < 3){
    if(numValid == 0){
        cin >> in1;
        numValid++;
    }
    else if(numValid == 1){
        cin >> in2;
        if(in1.compare(in2) == 0){
            cout << "The first and second entries match. Please try again" << endl;
        }
        else
            numValid++;
    }
    else{
        if(in1.compare(in3)) == 0 || in2.compare(in3) == 0){
            cout << "Your third entry matches a prior entry. Please try again" << endl;
        }
        else
            numValid++;
    }
}

cout << "You have chosen these lots: " << in1 << " " << in2 << " " << in3 << endl;