Aly Shaw
Aly Shaw

Reputation: 25

C++ how to make the user not repeat numbers

I need to make a program for a LoShu Magic Square. The User has to input the numbers 1-9, into the array, but they cannot repeat any of the numbers. I have tried using multiple while loops, but that didn't work completely. How would I solve this problem?

Upvotes: 2

Views: 53

Answers (1)

Khelifi Aymen
Khelifi Aymen

Reputation: 132

  1. define a vector to hold the entered data by the user ( initially it will be empty)
  2. use a do while loop as follow

do{ // get the user input }while(condition) // test if the entred data is in the vector, if it is not exit the loop // after exitting the loop add the element to the vector

This will ensure that the user want enter a number already exists in the vector. If you want the user to enter multiple numbers just add a uper while loop (infinite loop)

Upvotes: 1

Related Questions