-
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;
}
}
}
}
-
>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