+ 1
Help please 🥺..my loop is incrementing twice...for loop(int a=0; a<5; a++){cout<<"no. "<<++a; cin>>arr[a]; } //its ouput 1,3,5
C++
9 Réponses
+ 1
because of " cout<<++a; "
you must do " cout<<a+1; "
+ 3
FENG LI
your loop is incrementing twice because you have made it that way
when a = 0, ++a = 1 after printimg 1, it encounters a++ that made the value of a = 2 same for others
in short value of a is incrementing at ++a after that at a++
it totally depends on your logic,
btw what you want to do with the loop ??
👇👇👇
if you want to print numbers from 1 to 5 then write ( a + 1 ) in cout instead of ++a
+ 2
Ipang,NonStop CODING,
THANKS.
+ 1
Yes, because you increment "a" variable twice, once into the body of for loop and secondly in the parentheses of the loop.
IF you want to do like that you can do the following:
#include <iostream>
using namespace std;
int main() {
int arr[5];
for (int a=0; a<5;)
{cout<<"no. "<<++a;
cin>>arr[a];
}
return 0;
}
+ 1
I was printing to the screen the number
+ 1
Feng Li,
You misunderstood the purpose of relevant tags in the forum. Unlike in social medias, tags in forum serves a different purpose, to clarify context on language and/or topic; not for mentioning a friend. Putting your friend's name in post's tsgs will not send your friend any notification.
If you want to mention a ftiend, then you can do so by typing @ in post's Description textbox and choose a name from a popup list. Mind that mentions is only available in SoloLearn mobile app, not available in SoloLearn web.
https://code.sololearn.com/W3uiji9X28C1/?ref=app
0
U think?
0
Your code variable a increase twice bcz you first increment in for loop and then increment it in the body of for loop.
0
Wth