Help with if()

This is a discussion on Help with if() within the C++ Programming forums, part of the General Programming Boards category; Why wouldnt it break out of if() when i type "over" ? Code: // assignment operator with maps #include <iostream> ...

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

    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;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Why wouldnt it break out of if() when i type "over" ?
    It breaks out of the for loop, not the if block.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

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

    Thanks.
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21