Thread: Help with if()

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Help with if()

    Why wouldnt it break out of if() when i type "over" ?

    Code:
    // assignment operator with maps
    #include <iostream>
    #include <map>
    using namespace std;
    
    int main ()
    {
      string str;
      int i;  
      map<string,int> first;
      map<string,int>::iterator it;
      for(;;)
      {
             if (str == "over")
             {
             break;
             }
             else
             {
              cout<<"Enter your name: ";
              cin>>str;
              cout<<"Enter your age: ";
              cin>>i;
              first[str]=i;
             }
      }
      
      for ( it=first.begin() ; it != first.end(); it++ )
        cout << it->first << " => " << it->second << endl;
      
      
      system("pause");
      return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why wouldnt it break out of if() when i type "over" ?
    It breaks out of the for loop, not the if block.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Yes thats what i meant to say, sorry.

    But it doesnt break out of the for() loop it goes to else() always.
    Last edited by Ducky; 03-16-2008 at 03:37 AM.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ok sorry for the silly question, i found it, i misplaced the if statement.

    Thanks.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But it doesnt breaks out of the for() loop it goes to else() always.
    What do you mean?

    It works okay for me:
    Code:
    Enter your name: laserlight
    Enter your age: 123
    Enter your name: over
    Enter your age: 456
    laserlight => 123
    over => 456
    It looks like you probably want to ask the user if he/she wants to enter more input instead of using an input of "over" to signal end of input, but other than that it is working as expected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed