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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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++

24th Apr 2022, 3:34 PM
FENG LI
FENG LI - avatar
9 Answers
+ 1
because of " cout<<++a; " you must do " cout<<a+1; "
26th Apr 2022, 7:07 AM
Younes Salman
Younes Salman - avatar
+ 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
24th Apr 2022, 3:44 PM
NonStop CODING
NonStop CODING - avatar
+ 2
Ipang,NonStop CODING, THANKS.
25th Apr 2022, 7:26 AM
FENG LI
FENG LI - avatar
+ 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; }
24th Apr 2022, 3:40 PM
Black Winter
+ 1
I was printing to the screen the number
24th Apr 2022, 3:43 PM
FENG LI
FENG LI - avatar
+ 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
24th Apr 2022, 4:31 PM
Ipang
0
U think?
24th Apr 2022, 3:42 PM
FENG LI
FENG LI - avatar
0
Your code variable a increase twice bcz you first increment in for loop and then increment it in the body of for loop.
26th Apr 2022, 3:09 AM
Muhammad Rehan
Muhammad Rehan - avatar
0
Wth
26th Apr 2022, 6:15 AM
Julija Gromova