C++ Module Question: The For Loop no Input Problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++ Module Question: The For Loop no Input Problem

So, I have a question about the C++ Module for The For Loop. I managed to make the for loop for the module but when I put it in, there is no input on there but the output and expected output are the same. Does anyone know why that is happening? Also, I put a link to the code in case anyone wants to see it. https://code.sololearn.com/csEO9vkNXCoo/?ref=app

12th Sep 2020, 6:50 PM
Miss-Lydia
Miss-Lydia - avatar
11 Answers
+ 1
Infinite loop here 42 will be added everytime .just set condition to x<=dist;
12th Sep 2020, 7:11 PM
HBhZ_C
HBhZ_C - avatar
+ 1
HBhZ_C I did as you said. It is now saying no output. I even took out cin>>dist but still the same.
12th Sep 2020, 7:33 PM
Miss-Lydia
Miss-Lydia - avatar
+ 1
Miss-Lydia put @ before the name when you want mentionning someone "@"+name. In your code it outputs 0 because you enter dist value less than x so choose for example 98 and set x = 0;x < dist;
12th Sep 2020, 8:57 PM
HBhZ_C
HBhZ_C - avatar
+ 1
HBhZ_C Thanks for letting me know. I put the example and change the code. I got this. 0 40 80 https://code.sololearn.com/csEO9vkNXCoo/?ref=app
12th Sep 2020, 9:39 PM
Miss-Lydia
Miss-Lydia - avatar
+ 1
Ok Miss-Lydia Nice code you can use cin for user input and set condition logic too to avoid infinite loop(always condition is true) or illogical condition x> dist then x++ instead of x--
12th Sep 2020, 11:10 PM
HBhZ_C
HBhZ_C - avatar
+ 1
Jayakrishna🇮🇳 HBhZ_C I managed to solve the problem! Thank you very much! I really appreciate it! https://code.sololearn.com/c6E82uAGlKzD/?ref=app
13th Sep 2020, 7:59 PM
Miss-Lydia
Miss-Lydia - avatar
0
Your loop condition is either false always or true always.... Edit : What is your expected output? Miss-Lydia
12th Sep 2020, 6:59 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 The expected output is: 40 80 120
12th Sep 2020, 7:32 PM
Miss-Lydia
Miss-Lydia - avatar
0
Miss-Lydia #include <iostream> using namespace std; int main() { int dist=120; //cin>> dist; for (int x = 40; x <=dist;x += 40) { cout << x << endl; } return 0; }
13th Sep 2020, 1:04 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 I tried your code and I got: 0 40 80 Also, I think I figure out somewhat what I was doing wrong. The problem states you're on 5 hour ship ride and every hour that goes by, the ship is going 40 miles per hour. So, I think since my loop is not stopping at the fifth hour, it's not taking it.
13th Sep 2020, 6:16 PM
Miss-Lydia
Miss-Lydia - avatar
0
Miss-Lydia You're welcome... And My code gives output 40 80 120 which is you asked.. Since you not given problem description earlier, I just modified to get desired output only... Well you managed it.. Adding more details will help you get quick accurate answers.. Try from next time.. Happy learning....
14th Sep 2020, 2:00 PM
Jayakrishna 🇮🇳