Thread: finding already entered number

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Unhappy finding already entered number

    How do make sure someone doesn't enter the same number more than once?

    This code doesn't work. I'm not sure what I'm doing.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    	int number;
    
    	cout << "Enter five numbers, and don't enter the\n"
    		 << "same number more than once" << endl;
    
    	for (int i = 0; i < 5; i++)
    	{
    		cin >> number;
    		
    		for (int repeat_number = 0; repeat_number < number; repeat_number++)
    
    		if (repeat_number == number)
    		{
    			cout << "You've already entered that number" << endl;
    			i--;
    		}
    	}
    
    
    	return 0;
    
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Just use an array, store the values in it, and check whether the value is in the array.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    18
    Your code doesn't work because you are only checking repeat_number against number, which is the last entered number, each time you go through the loop. What you need to do is check it against all previously entered ones. To get around this, you would want to consider using an array of numbers, and checking each entry against that array, then adding it to the array. If you don't know arrays yet, just use five different variables, and compare all of them to each other.
    ~Sean Michael Simonsen

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    here, take a look at this:

    Code:
    #include <iostream>
    
    int main()
    {
    	int number, repeat, i, j;
    	int array[6];
    	
    	std::cout << "Enter five numbers." << std::endl;
    	
    	for(i = 0; i < 5; ++i) {	
    		std::cout << "Enter number " << i + 1 << ": ";
    		std::cin >> number;
    	
    		repeat = number;
    		array[i] = number;
    		
    		if(i > 0) {
    			for(j = 0; j < i; ++j) {
    				if(array[j] == number) {
    					std::cout << "Number repeated." << std::endl;
    					exit(1);
    				}
    			}
    			array[i] = number;
    		}
    	}
    	
    	for(i = 0; i < 5; ++i) {
    		std::cout << "Number " << i + 1 << " = " << array[i];
    		std::cout << std::endl;
    	}
    	
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding the least bigger number from the tree
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 04-12-2009, 03:34 PM
  2. Replies: 7
    Last Post: 04-12-2009, 12:40 PM
  3. finding the element number in an array
    By tommy69 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 04:26 AM
  4. Finding the number of different paths on a rectangular grid
    By joejoefla in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2004, 02:13 PM
  5. finding the decimal part of a number
    By Geo-Fry in forum C++ Programming
    Replies: 13
    Last Post: 07-31-2003, 12:43 PM