How do I stop a for loop that stores the values of strings typed in C++?

Asked

Viewed 326 times

2

So I’ve tried everything I’ve tried to compare type typed a "." or set a limit value and even then the program does not continue, it stays inside the infinite loop. The code comes next:

vector<string> inserir(){
    vector<string>f;
        for(int x = 0;x < 5;x++){
        string s;
        cin>>s;
        f.push_back(s);
    }
    return f;
}

can anyone help me? Someone who has already had the same problem?

  • 1

    Just to understand his code is stuck in the for(... ? Strange why should he loop 5 times as you set. Check if it is right there that it is locking.

  • it can only be there because it dps to type 5 words it continues on Cin waiting to type more

  • I realized now that you are using Cin>s; and the program will stop on this line waiting for you to enter some data with the keyboard and wait for Enter to proceed.

  • I use eclipse, I didn’t rule out the possibility of it being him doing grace

  • tried on another ide gave the same thing...

  • 1

    use Std::getline(Std::Cin, s) instead of Cin >> s.

  • so when I want to read a string I use getline()?

  • put a complete (small) example...the code you posted doesn’t seem to have errors

  • http://cpp.sh/9x2n3 posted c++ shell to facilitate

Show 4 more comments

1 answer

1

Your code in http://cpp.sh/9x2n3 has a line like this

for (int y = 0; y < med.size(); x++) {

This line repeats indefinitely. Obvious correction:

for (int y = 0; y < med.size(); y++) {
  • I don’t know what you mean

  • you are y to control the loop, but is increasing x...the value of y will remain zero, and the output condition of the loop will never occur...more than this I do not know how to explain

  • I get it, thank you

Browser other questions tagged

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