Я пытаюсь выяснить, был ли лучший или лучший тестовый цикл, чтобы пользователь мог продолжать вводить значения, которые будут искаться, до тех пор, пока не будет введено значение дозорного знака для завершения программы. Кроме того, как бы выглядели мои параметры для цикла? Вот мой код, мне нужно включить цикл. Кроме того, я понимаю, что цикл пост-теста выполняется хотя бы один раз. Заранее спасибо!Предварительная или пост-тестовая петля?
#include<iostream>
using namespace std;
int searchList(int[], int, int); // function prototype
const int SIZE = 8;
int main()
{
int nums[SIZE]={3, 6, -19, 5, 5, 0, -2, 99};
int found;
int num;
// The loop would be here
cout << "Enter a number to search for:" << endl;
cin >> num;
found = searchList(nums, SIZE, num);
if (found == -1)
cout << "The number " << num
<< " was not found in the list" << endl;
else
cout << "The number " << num <<" is in the " << found + 1
<< " position of the list" << endl;
return 0;
}
int searchList(int List[], int numElems, int value)
{
for (int count = 0;count <= numElems; count++)
{
if (List[count] == value)
// each array entry is checked to see if it contains
// the desired value.
return count;
// if the desired value is found, the array subscript
// count is returned to indicate the location in the array
}
return -1; // if the value is not found, -1 is returned
}
'int ++' не будет компилироваться. –
'if (int i = 0; num> 0; int ++)' Вы имеете в виду 'for (...)'? Лучший способ найти язык - это купить [хорошую книгу] (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) и проработать упражнения. У вас будет больше шансов ответить на ваш собственный вопрос, если вы лучше поймете основы. – anjruu
Вы также проверяете 'num> 0' перед присвоением значения' num'. –