-1

I am trying to figure out how to block out the same input twice or more by a user. For example, the program requires the user to input 3 inputs and not the same inputs. But for now my program still proceeds with choosing the same input. If the user inputs B2 3 times then it chooses it 3 times. The output will be You have chosen these lots: B2 B2 B2 which should not be possible.

my input is array btw. code[3].

I want it to be able to read the same input previously and say that it has already been chosen, choose another one as input.

I prefer using 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));
}
iDamn
  • 11
  • 2
  • Please [edit] your question and include your code. – anatolyg Jun 03 '18 at 14:19
  • alright edited it, @anatolyg – iDamn Jun 03 '18 at 14:40
  • 3
    Put the strings in an `std::set`. While the set’s size is less than `n` read another answer. While the set contains an answer don’t accept it. – Biffen Jun 03 '18 at 15:06
  • 2
    Please add your definition/declaration of `code`. You added `code[3]` in text, but it's clearer to put it in code. Is it `string code[3]`? I am trying to guess; better prevent people from guessing, and post your full code. Including and `#include `, and all. See [mcve]. – anatolyg Jun 03 '18 at 15:15
  • sorry for my ignorance @anatolyg, ive edited it again with string(char) declaration and the definitions. thats all that i think related to this part of the program. it's also in a function if that matters which shouldnt – iDamn Jun 03 '18 at 15:27
  • 1
    Life would be easier using `std::string` rather than character arrays. For example, you can use `operator==` for comparison with `std::string`. – Thomas Matthews Jun 03 '18 at 17:58

3 Answers3

1

If you have to look at the problem a bit differently, you will see that there is no need for strings or higher-level data structures at all.

The inputs are all one character and one digit. If you read this as one character and one digit, you can use a 2D array,

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

To store the state of any of the lots. inuse[character][digit-1] tells you whether or not that particular character and digit combination have been seen and set before.

Wrapped around that is the usual input validation to make sure the user typed in something usable.

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
user4581301
  • 33,082
  • 7
  • 33
  • 54
0

To check whether second input is equal to first input:

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

To check whether third input is equal to the first or second input, quite literally:

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

However, if you want to extend it to more than 3 inputs you should hold a list of all earlier inputs (as suggested by 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";
}

This code (requires using the C++ Standard Library) inserts the new input into the set of all earlier inputs. The result is a data structure, whose second element tells whether the insert operation succeeded (i.e. whether the new input didn't already appear in the set).

This slightly obscure code is ultimately intended for best performance; there are some other ways to implement this - see this related answered question.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • yes i prefer the first method, and i did in fact tried that before but to my surprise it just gave me an infinite loop. even the FIRST input that i typed in just reads as the same input entered previously which is none. need more help similar to the first method cause im not familiarized or taught with the latter method yet – iDamn Jun 03 '18 at 22:53
-1

Try this:

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;
Amanda
  • 314
  • 3
  • 12
  • 2
    Several problems here, such as: (1) `operator<<` doesn't work with `cin` (2) `strcmp` arguments are `const char*` not `std::string` – Ben Voigt Jun 03 '18 at 14:30
  • its worth mentioning that im using an array for said input `code[a]`. so what can i do there? im not familiar with your method. very newbie – iDamn Jun 03 '18 at 14:35
  • Thank you @BenVoigt, I've been made soft by IDE autocorrect and I fixed my mistakes. – Amanda Jun 03 '18 at 20:33