Here It is for Bingo game. Here i am going to get 9 number ie 1-9. if he/she enter 1 then it check whether it is located in a or not.If it is present then replace it with 0.Upto this it is ok but if the enter number is not present i want to display that enter another number which is not present in array n want to get that number and to continue the process. How to do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int a[3][3]={1,2,3,4,5,6,7,8,9},k;
cout<<"Enter a number";
for(int l=0;l<9;l++)
{
cin>>k;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
{
if(a[i][j]==k)
{
cout<<"\n Enter next number";
a[i][j]=0;
}
}
}
}
First off I would highly recommend you ditch the multi demensional array and go for a standard vector.
This would work much easier and save you some problems later on. std::vector<int> board = {1, 2, 3, 4, 5, 6, 7, 8, 9};
That would also open up some possibilities for your current problem. Like using any_of like so
1 2 3 4 5 6 7 8 9 10 11 12
int guess;
cin >> guess;
if (!std::any_of(board.begin(), board.end(), [guess] (int i) { return i == guess; }))
{
std::cout << "That number is not on the board! Please enter another number" << std::endl;
std::cin >> guess;
}
else
{
// Do whatever happens when the user guesses a number on the board.
}
This code doesn't take into account multiple guesses but it shows a possible option.
Though that is a bit complicated I admit so your best bet is to just loop through your array or vector and check if the number the user entered matches any of the numbers in the array or vector. You can do this with a function if you know about them yet which would be easiest or you can just do it with loops and a conditional variable.
for example
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int myArray[3] = {1, 2, 3};
int guess;
cin >> guess;
bool found = false;
for (int i = 0; i != 3; ++i)
{
if (myArray[i] == guess)
{
found = true;
break;
}
}
That is probably the most simple solution but there are many more out there also.
while running above program there is no header file for the above program ... That is it showing that there is no algorithm header file is available.. where to run these program ???
Are you sure that you are compiling this in C++ and not C? What compiler are you using? This file is in the standard library and has been since at least C++03.