C++ 不允许在C++;

C++ 不允许在C++;,c++,strcmp,C++,Strcmp,我试图找出如何阻止用户输入两次或更多次相同的内容。例如,程序要求用户输入3个输入,而不是相同的输入。但现在我的程序仍然继续选择相同的输入。如果用户输入B23次,则选择它3次。输出将是您选择了以下批次:B2,这应该是不可能的 我的输入是数组btw。code[3] 我希望它能够读取相同的输入之前,并说它已经被选中,选择另一个作为输入 我更喜欢使用strcmp #include <iostream> #include <iomanip> #include <string.

我试图找出如何阻止用户输入两次或更多次相同的内容。例如,程序要求用户输入3个输入,而不是相同的输入。但现在我的程序仍然继续选择相同的输入。如果用户输入
B2
3次,则选择它3次。输出将是
您选择了以下批次: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][10];
对于(int a=0;a ws;
cin.getline(代码[a],10);
如果((strcmp(代码[a],“A4”)==0)| | |(strcmp(代码[a],“A6”)==0)| | |(strcmp(代码[a],“B1”)==0)))
{
试试这个:

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;
int numValid=0;
字符串in1、in2、in3;
而(numValid<3){
如果(numValid==0){
cin>>in1;
numValid++;
}
else if(numValid==1){
cin>>in2;
如果(in1.比较(in2)==0){

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

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

cout如果您必须以不同的方式看待这个问题,您将看到根本不需要字符串或更高级别的数据结构

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

bool inuse[2][7] = {};
存储任何批次的状态。
使用[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
char组;
无符号整数索引;
cin>>组>>索引;//获取一个字符和一个数字
if(cin)//以上读取成功,数据良好。
{

如果((组=='A'或组=='B')和(索引>0和索引请将您的问题和代码包括在内。好的,编辑它,@anatolygp在
std::set
中输出字符串。当set的大小小于
n
时,阅读另一个答案。当set包含答案时,不要接受它。请添加
code
的定义/声明。您添加了
code[3]
在文本中,但在代码中更清晰。它是
字符串代码[3]
?我正在尝试猜测;更好地防止人们猜测,并发布您的完整代码。包括和
#包括
,等等。请参阅。使用
std::string
比使用字符数组更容易。例如,您可以使用
操作符==
std::string
进行比较。这里有几个问题,例如as:(1)
运算符值得一提的是,我使用一个数组作为所述输入
code[a]
。那我该怎么办呢?我对你的方法不太熟悉。非常新谢谢你@BenVoigt,我被IDE自动更正弄软了,我修正了我的错误。是的,我更喜欢第一种方法,事实上我以前也试过,但令我惊讶的是,它只给了我一个无限循环。即使是我键入的第一个输入,也只能读作相同的输入e我以前学过,现在还没有。需要更多类似于第一种方法的帮助,因为我还不熟悉第二种方法
bool inuse[2][7] = {};
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