My program made in C++ is in infinite loop, how to fix?

Asked

Viewed 224 times

-4

My program skips right to the end. Doesn’t even enter to read.

Follows code:

#include <iostream>

int main(void) {

int i= 0;
string nome;
for(i=0;i>=10;++i) {
     cin>>nome;
     cout<<nome<<endl;
}

}

I can’t find the error.

  • You want how many names?

  • 10 names. the answer has been answered

  • 1

    So the answer you accepted is wrong, I’ll answer

4 answers

8

The operator used is wrong, needs to be a <( less than). If you start from 0 and go up to 10, and increment you can not check if the number is greater than a certain value to determine whether it will continue. Maybe the confusion is because you don’t understand well how the for, there has to be true to continue running, so when it is false it closes the loop of repetition. The correct is to check if the number is still less than the desired target value which is 10.

There you may wonder why it should be only "less than" and not "less than or equal to". Because you started from 0, and you’re right to start from this number, not that it makes so much difference, the important thing is that it runs 10 times, because that’s the problem statement, so you can start from the number you want and go to the number fitting for this, but it was agreed to start from 0, so the condition is true to number 9, since 0 counts too. Therefore the sign must be only smaller, when it is equal, or if it has reached the 10, it must be false, because starting from the 0 the number 10 is the eleventh, one more than you want. So use it to <= is wrong and the answer accepted and voted is wrong.

#include <iostream>
#include <string>
using namespace std;

int main() {
    for (int i = 0; i < 10; i++) {
        string nome;
        cin >> nome;
        cout << nome << endl;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

How curiosity could do this:

for (int i = 1; i <= 10; i++)

There the <= would be correct because it started from 1, but this is not how programmers do, leaving the pattern creates difficulties of understanding. We write codes for everyone to understand and so it is better to follow the standards already adopted.

4


Good friend, from what I see, your condition in the is will never be true and will enter the loop.

Because of the for(i=0;i>=10;++i) she doesn’t even get in, why i = 0 and you ask i is greater than or equal to 10, then that condition will never be true and nor will it enter the loop

You can change by for(i=0;i<=10;++i) and so it will work properly.

  • Read the cycle for as follows that is able to help you friend. O iis equal to zero (i=0) and as i is less than or equal to 10 (i<=10) add/increment the variable i (++i)

0

As it is, its function will not enter the loop because the variable 'i' starts with 0 and will never meet its condition of being greater than or equal to 10.

for(i=0;i>=10;++i)

Therefore, in fact your code is not in "infinite loop", it simply does not enter the 'for' and does nothing.

0

Your operator has the wrong signals, change the line:

for(i=0;i>=10;++i) {

To:

for(i=0;i<10;++i) {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.