Thread: c++ help

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Eindhoven, Noord-Brabant, Netherlands
    Posts
    54

    c++ help

    Hi, guys, I need a little help. My program closes after the out even though i've used cin.get():. Can someone please help me? I want the program to stay open at least until you press enter, even after the whole output(dunno if i'm makin sense).
    Please help me. Here is the code:
    Code:
     #include <iostream>#include <string>
    int main()
    {
        std::cout << "Please enter your name";
         std::string name;
        std::cin>> name;
         // build the message that we intend to write
          const std::string greeting = "Hello, " + name + "!";
          //build the second and fourth lines of output
           const std:: string spaces(greeting.size(), ' ');
           const std::string second = "* " + spaces + " *";
           //build the first and figth lines of the output
           const std::string first(second.size(), '*');
          //write it all
           std::cout << std::endl;
           std::cout << first << std::endl;
           std::cout << second << std::endl;
           std::cout << "*" << greeting << "*" << std::endl;
           std::cout << second << std::endl;
            std::cout << first << std::endl;
            
                   std::cin.get();
                   
                   
                   return 0;
              
                   
                   }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use cin.ignore() before to get rid of \n left by the previous input operation.

    and work on your indentation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Eindhoven, Noord-Brabant, Netherlands
    Posts
    54
    Quote Originally Posted by vart View Post
    use cin.ignore() before to get rid of \n left by the previous input operation.

    and work on your indentation
    r u playin or serious, dog? Can u please at least write how i can so it, can u plz write the code? Thx in advance.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Location
    Eindhoven, Noord-Brabant, Netherlands
    Posts
    54
    Quote Originally Posted by vart View Post
    use cin.ignore() before to get rid of \n left by the previous input operation.

    and work on your indentation
    thx a lot, dude

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, it's serious.
    You should at least be able to search for cin.ignore() to either find the manual page and/or find some examples of it's use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed