Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. while error ?

while error ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++game-devhelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bluatigro
    wrote on last edited by
    #1

    this program does not halt why ?

    //les02for.cpp
    #include
    #include
    using namespace std ;
    int main()
    {
    string in = " ";
    while ( in != "" )
    {
    cout << "What is your name ? \n" ;
    cout << "[ Pres enter to end this .]\n" ;
    cin >> in ;
    cout << "Hello " << in << " .\n" ;
    }
    cout << "n\n\n\[ GAME OVER ]\n" ;
    cin.get() ;
    return 0;
    }

    D M 2 Replies Last reply
    0
    • B bluatigro

      this program does not halt why ?

      //les02for.cpp
      #include
      #include
      using namespace std ;
      int main()
      {
      string in = " ";
      while ( in != "" )
      {
      cout << "What is your name ? \n" ;
      cout << "[ Pres enter to end this .]\n" ;
      cin >> in ;
      cout << "Hello " << in << " .\n" ;
      }
      cout << "n\n\n\[ GAME OVER ]\n" ;
      cin.get() ;
      return 0;
      }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      bluatigro wrote:

      why ?

      Have you stepped through it using the debugger (to see what value in has when you think it should be 'empty')? The string class may not have a != operator so what you may be comparing is the object itself against a string literal (rather than the contents of the object).

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      M 1 Reply Last reply
      0
      • D David Crow

        bluatigro wrote:

        why ?

        Have you stepped through it using the debugger (to see what value in has when you think it should be 'empty')? The string class may not have a != operator so what you may be comparing is the object itself against a string literal (rather than the contents of the object).

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        M Offline
        M Offline
        Manish K Agarwal
        wrote on last edited by
        #3

        std::string have != operator. The problem is with cin, it does not takes new line char or leading space chars by default.

        1 Reply Last reply
        0
        • B bluatigro

          this program does not halt why ?

          //les02for.cpp
          #include
          #include
          using namespace std ;
          int main()
          {
          string in = " ";
          while ( in != "" )
          {
          cout << "What is your name ? \n" ;
          cout << "[ Pres enter to end this .]\n" ;
          cin >> in ;
          cout << "Hello " << in << " .\n" ;
          }
          cout << "n\n\n\[ GAME OVER ]\n" ;
          cin.get() ;
          return 0;
          }

          M Offline
          M Offline
          Manish K Agarwal
          wrote on last edited by
          #4

          To make it working, change as following:

          #include
          #include
          using namespace std ;
          int main()
          {
          string in = " ";
          while ( in != "" )
          {
          cout << "What is your name ? \n" ;
          cout << "[ Pres enter to end this .]\n" ;
          getline(cin, in );
          cout << "Hello " << in << " .\n" ;
          }
          cout << "n\n\n\[ GAME OVER ]\n" ;
          cin.get() ;
          return 0;
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups