1

I am trying to figure out whether a pre or post test loop would be the best method so that the user can continue to input values that will be searched for, until a sentinel value is entered to end the program. Also, what would my parameters look like for the loop? Here is my code, I need to include the loop. Also, I understand that a post test loop is executed at least once. Thanks in advance!

#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
  }
Allexey
  • 33
  • 1
  • 9
  • 2
    `int++` will not compile. – Zac Howland Nov 18 '13 at 18:46
  • 2
    `if (int i=0; num > 0; int++)` Do you mean `for(...)`? The best way I've found to learn a language is by buying a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and working through the exercises. You'll have a better chance of answering your own question if you have a better grasp on the basics. – anjruu Nov 18 '13 at 18:51
  • You also check `num > 0` before assigning a value to `num`. – Jim Rhodes Nov 18 '13 at 18:53
  • @ZacHowland I know, I was trying to figure out if a pre or post loop would work and was doing guess work, I will edit and delete that attempt – Allexey Nov 18 '13 at 18:53
  • Yeah, when supplying actual code it's typically best to make sure it actually compiles, otherwise your intention is unclear. Like for example, your "if (int i=0; num > 0; int++)" line needs brackets otherwise the "cin" line below it won't be part of that conditional, secondly shouldn't it be "while(num != SENTINEL)" or something? – jaypb Nov 18 '13 at 18:56
  • Pre or post test on what? Also, `count <= numElems` should be `count < numElems`. – Jim Rhodes Nov 18 '13 at 18:57
  • @BaylesJ It now compiles. The loop I attempted was guess work that I forget to take out before posting. Thanks! – Allexey Nov 18 '13 at 18:57
  • @JimRhodes I need to figure out if a pre test loop or a post test loop would be most beneficial for the user to enter numbers, so the program can search the array, until they use a sentinel to stop the loop. – Allexey Nov 18 '13 at 18:59
  • This question should be re-opened because it includes a code that can be referenced and a relevant question. It is not asking anyone to write the code, just simply a question with two possible answers. It does not lack understanding from the user and it can be beneficial to others. Isn't Stack Overflow a community to help others? This question does just that. Engagement and interaction between users has resulted in a valid answer, useful to this community. – Allexey Nov 21 '13 at 21:11

2 Answers2

3

Your question is more of a use case dependent.

Post Case: When you need the loop to run AT LEAST once ( 1 or more times)

Pre Case: The loop can run 0 or more times.

Rahul Shardha
  • 399
  • 7
  • 16
1

I must say, I'm not entirely sure what you want to know. I would honestly recommend a good book on C++. Post test loops are not super popular in C++ (they are of the form "do.. while" where "while" loops / pre test loops are much more common). A little more information is available here: "Play It Again Sam"

EDIT: you need to get data from the user, test it, and then do stuff based on it. Your best bet is something along the lines of

 static const int SENTINEL = ??;
 int num;

 cout << "please input a number" << endl;
 cin >> num;
 while( num != SENTINEL ) {
      // DO STUFF HERE

      // Now get the next number
      cout << "please input a number" << endl;
      cin >> num;
 }
Community
  • 1
  • 1
jaypb
  • 1,544
  • 10
  • 23
  • one more thing, when I tried the while loop, and used -99 as the SENTINEL, the loop didn't exit until I entered another number and then it exited. Why is that? – Allexey Nov 18 '13 at 19:14
  • It has to do with when you check the values of num; I updated my code above and that might help. – jaypb Nov 18 '13 at 19:28