Hello everyone, Ive been stuck with this little problem for quite some time and if someone could help id be much obliged. I have this code that contains an array with several numbers in put by the user and i don't want any of the numbers in the array to equal one another.But with what I've come up with below has a slight problem in the fact that it tests all numbers in the array including the number that has just been entered creating an infinite loop >.< This is driving me crazy someone save me!

//-----initialize MovieChoice array
       int MovieChoice[net.NUMMOVIES];
       for (int i=0; i < net.NUMMOVIES; i++)
         {  
                MovieChoice[i]=-1;
         }
       //-----------------------------
       
	   
       //-----Prints available movies
       cout << "Please choose one of the following videos" << endl;
	
	   for (int j = 0; j < net.NUMMOVIES; j++)
   	   {    
         cout << j << " for ";	
         net.video[j].Print();
       }
   	   //------------------------  
   	  
   	  //---------------------------------
   	  for(int j=0; j < net.NUMMOVIES;j++)//Puts video choices in array 
   	  {                                  //so there are no dupes
           while(MovieChoice[j-1] != 9)
           {
            cout << endl << "Which movies would you like?(Please enter number of movie one at a time)";
            cin >> MovieChoice[j];
            
            if(MovieChoice[j] == 9)//breaks out of loop if user presses 9
            {
               break;
            }
            
            for (int h =0; h < net.NUMMOVIES; h++)//error test for similar number in array
              {
                     
                while (MovieChoice[j] == MovieChoice[h])
                  {   
                  cout << endl << "That movie has already been selected. Press 9 to calculate or choose another movie: ";
                  cin >> MovieChoice[j];
                  }
              }
            while ((MovieChoice[j] > net.NUMMOVIES || MovieChoice[j] < 0))//error test for number
            {   
                cout << endl << "Invalid! Which movies would you liketo rent?(Please enter numbercode of movie one at a time)or enter 9 to calculate: ";
                cin >> MovieChoice[j];
            }
            
            
            net.video[MovieChoice[j]].Print(); 
            cout <<  "Selected\n";
            Cost = Cost + net.video[MovieChoice[j]].GetPrice();
              
        }  
        cout << "\nYour total is: " << Cost;
        
      }
      return Cost;

Recommended Answers

All 2 Replies

Can you separate your action into a function that only does that one thing?
If you are attempting to ensure numbers in an array are all unique, it's easy to do if you're not concerned about the display and only have the function return if a number was found MORE THAN ONCE.

Ahh and then i could just use that to compare a value to instead. Thanks for that, i guess i just needed that little extra push. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.