sentinel

This is a discussion on sentinel within the C++ Programming forums, part of the General Programming Boards category; I have a question about how I can take a negative number to exit the loop without passing it to ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    sentinel

    I have a question about how I can take a negative number to exit the loop without passing it to the sortarray function. I was going to use a sentinel but I'm stumped and need some suggestions.

    #include <iostream.h>
    #include <iomanip.h>

    void sortarray(int [], int);

    void main(void)
    {
    int count,k,tscores = 0;
    int test[100];

    for(count=0;count <100 ;count++)
    {
    cout << "\nEnter a number:";
    cin >> test[count];

    if(test[count]<0)
    {
    test[count-1];
    break;
    }
    }
    sortarray(test,count);

    for(k=0;k<count;k++)
    {
    cout << test[count] <<endl;
    }

    }

    void sortarray(int nums[],int count)
    {
    int hold,a,b;
    for(a=0;a<count-1; a++)
    {
    for(b=a+1; b<count; b++)
    {
    if(nums[a]<nums[b])
    {
    hold = nums[a];
    nums[a] = nums[b];
    nums[b] = hold;
    }
    }
    }
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if(test[count]<0)
    >{
    >test[count-1];
    >break;
    >}
    What is this supposed to do? You look at the value of test[count-1] and do nothing with it then break. And do you want to exit the loop but not the program altogether? If not you can easily do it by saying:
    Code:
    if(test[count]<0) 
      return;
    Otherwise you can give the user an option after you break from the loop:
    Code:
    cout<<"Sort the data? (y/n): ";
    cin>>flag;
    if ( flag == 'y' )
      sortarray( test, count );
    Btw, don't use void main. The proper definition of main is int main ( void ).

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sentinel controlled repetition example
    By droseman in forum C Programming
    Replies: 7
    Last Post: 09-26-2008, 02:17 AM
  2. Problem with sentinel values
    By Golfduffer in forum C Programming
    Replies: 3
    Last Post: 08-30-2007, 01:19 AM
  3. SENTINEL question
    By codeHer1 in forum C Programming
    Replies: 2
    Last Post: 03-24-2005, 08:34 AM
  4. Sentinel while loop and errors.
    By lollypop in forum C Programming
    Replies: 12
    Last Post: 10-19-2003, 12:40 PM
  5. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 10:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21