#include
using namespace std;
int main()
{
int c,a[10],flag=0,k=0;
for(int i=0;i<10;i++)
a[i]=0;
while(1)
{
cin>>c;
if(c==42)
flag=1;
else if(flag==0)
{
a[k]=c;
k++;
}
else
break;
}
for(int i=0;i<k;i++)
cout<<a[i]<<"\n";
return 0;
}
change the array size. in this question, it is not necessary that the number of inputs is less than or equal to 10. your code will only work for 10 inputs else run-time error
Your code is wrong because it is valid only for input size 10, as you have declared the array of size 10 and after that in the while loop if your k increases from 10 for a[k] then it will give error.
Means simply, if you have inputs less than 10 in the while loop ( Please don’t forgot that you can only enter only one more input after input 42, so your total inputs should be less than 42 than no error else if input is more than 10 or input contains 42 at 10 position (according to user) then there will be error.
Please see the no error eg. of your code: https://ideone.com/rqg0HZ
Hoping that you understands it.
you dont need to store the numbers in an array, you just have to stop processing when you encounter 42!
pseudocode:
1) take input
2) check if 42
3) break
4) else
5) print the number
6) take another input
Thank you sir.
thank you sir
thank you sir