I know you guys hate goto statements, but would you consider this a good use of a goto statment:
Code:
#include <iostream>
using namespace std;
int search(long id_nums[], int size, long value);
int main()
{
long id_nums[] = {5658845, 8080152, 1005231,
4520125, 4562555, 6545231,
7895122, 5552012, 3852085,
8777541, 5050552, 7576651,
8451277, 7825877, 7881200,
1302850, 1250255, 4581002,};
long number;
int result;
cout << "Enter a number" << endl;
RESTART: cin >> number;
result = search(id_nums, 18, number);
if (result == -1)
{
cout << "Invalid number. Enter a valid number" << endl;
goto RESTART;
}
else
cout << "Your number is valid" << endl;
return 0;
}
int search(long id_nums[], int size, long value)
{
int i = 0;
int position = -1;
int found = 0;
while (i < size && !found)
{
if (id_nums[i++] == value)
{
found = 1;
position = i;
}
}
return position;
}