Thread: How to stop user from entering same number twice?

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    13

    How to stop user from entering same number twice?

    i need to create a program that will accept 5 numerical inputs and determine the highest, and i also need to stop user from using the same number twice and do not count it from the 5 numerical inputs. kindly help me, here is my code
    Code:
    int number,x,j=5;
    int max=0;
    int main(){
        do{
        for(int i=0; i<j; i++)
        {
        cout<<"\nInput number :";
        cin>>number;
        if(number>max)
        {
        max = number;
        x=0;
        }
    
        else if (number == max)
        {
        cout<<"\nDo not use the same number twice! Please Try again!";
        i--;
        x=1;
        }
        }
    
        }while(x==1);
    
        cout<<"The highest number is: "<<max;
    
        getch();
        clrscr();
        return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You may find it useful to store the numbers you've read so far in an array, then search the array to see if the user has entered any duplicates. The getch() and clrscr() functions are non-standard and usually only exist on very old compilers. Consider using cin.get(); instead and maybe using a modern compiler, it may make your life easier by providing e.g. better warnings and error messages.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    You can use set<int> and check the std::set's insert signature if insertion was successful or not
    Code:
    #include <iostream>
    #include <set>
    
    
    using namespace std;
    const int SIZE = 5;
    
    
    int main()
    {
        set<int> numbers;
        int counter{};
        do
        {
            cout << "Enter number: \n";
            int num;
            cin >> num;//recommned some input validation as well;
            if(numbers.insert(num).second)
            {
                counter++;
            }
            else
            {
                cout << "Duplicate number, try again \n";
            }
        } while (counter < SIZE);
    }

  4. #4
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    oops Sean

    Code:
    int counter{};

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    value initialization - cppreference.com
    Though to be consistent I should probably have also used
    Code:
    constexpr auto SIZE = 5;

  6. #6
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    I knew that was added for class types in C++ 11 but I've never seen initialisation list used to initialise a single primitive type like that. Learn something new every day. I don't really like it personally as I prefer my code to read like English. I would have used the good old fashioned = 0 notation myself.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It's not an initialization list. It's a new constructor syntax. And it does lead to some nasty unexpected consequences with regards to std::vector but that's about it.

    You can now just type:
    Code:
    int x{1337};
    // vs
    int y(1337);
    Now the difference pops a lot more.

  8. #8
    Registered User
    Join Date
    Oct 2015
    Posts
    13
    Hey guys, I forgot to tell you that I am obliged to use Turbo C Compiler.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Whoever is mandating that you to use Turbo C is doing you an extreme disservice. That compiler has been obsolete for decades.

    Do yourself a favor - get a modern book, get a compiler/IDE from this millennium, and continue the learning process on your own. You can still "fake it" in class if you need to make grades.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Entering valid user input?
    By Frank1234 in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2013, 11:42 PM
  2. Replies: 3
    Last Post: 02-03-2013, 06:27 AM
  3. Entering a user equation to a source function ?
    By dcuneo in forum C++ Programming
    Replies: 5
    Last Post: 03-01-2008, 05:38 AM
  4. preventing user entering characters
    By Ashkan in forum C Programming
    Replies: 12
    Last Post: 08-24-2003, 12:56 PM
  5. User Entering Numbers
    By ct26torr in forum C Programming
    Replies: 1
    Last Post: 01-26-2003, 01:40 PM

Tags for this Thread