Generic doubt regarding array input

when I use a for loop for taking the input it works fine as expected. but when I use a while loop like:
int n; cin >> n;
int i = 0;
while(n–){
cin >> a[i];
i=i+1;
}
the input is not being taken properly. is there any reason for this?

@preritpathak
it is working fine

you can also see here

You’re printing it inside the while loop. It doesn’t work if you try printing the elements after you’ve taken the input for the array. example : https://ide.codingblocks.com/s/237398

@preritpathak

as you are deceasing n so at the end of loop n=0
hence next loop will not execute
for that make a change n=i; after 1 loop

i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course

1 Like