Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
main()
{
    int a[20],x;
    int n=a[0];

        for(x=0; x<20; x++)
         {
           cout<<"Enter integer : ";
           cin>>a[x];


           while(a[x]<0)
            {
              cout<<"Enter integer : ";
              cin>>a[x];
            }
         }


This should be the output

Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: -1
Input: -1
Input: -1
Input: 1
Input: -1
Input: 1
Input: -1

Stop!
Posted

1 solution

use a counter variable inside the for loop
C++
int countNegative=0
if(a[x]<0)
 countNegative++

use a break statement when the countNegative becomes 5

C++
if(countNegative==5)
 break;
 
Share this answer
 
v2
Comments
Safe Search 26-Jul-14 5:46am    
Thank you very much sir.
Abhijit Ghosh (Subho) 26-Jul-14 5:52am    
You are welcome.
Rate the answer if it solves your issue.
Abhijit
Safe Search 26-Jul-14 6:52am    
Sir my another problem is when i display the input numbers it displays unwanted numbers and how can i display the input numbers properly?
Abhijit Ghosh (Subho) 26-Jul-14 6:57am    
What do you mean by unwanted numbers?
Safe Search 26-Jul-14 7:10am    
Like this sir...

Enter integer 0 : 1
Enter integer 1 : 1
Enter integer 2 : 1
Enter integer 3 : —1
Enter integer 4 : —1
Enter integer 5 : —1
Enter integer 6 : —1
Enter integer 7 : —1
-1
-1
-1
-1
-1
31619
293384
421 62
1200101
-1
5535
293364
4230143
1233024

The condition to terminate the input has been satisfied.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900