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
11 ответов
+ 1
Infinite loop here 42 will be added everytime .just set condition to x<=dist;
+ 1
HBhZ_C I did as you said. It is now saying no output.
I even took out cin>>dist but still the same.
+ 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;
+ 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
+ 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--
+ 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
0
Your loop condition is either false always or true always....
Edit :
What is your expected output? Miss-Lydia
0
Jayakrishna🇮🇳 The expected output is:
40
80
120
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;
}
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.
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....