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. How to limit cin to string?

How to limit cin to string?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
5 Posts 5 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.
  • H Offline
    H Offline
    hanlei0000000009
    wrote on last edited by
    #1

    char str[8];
    cin >> str;

    When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!

    _ T R D 4 Replies Last reply
    0
    • H hanlei0000000009

      char str[8];
      cin >> str;

      When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      allocate memory dynamically based on the size of the string You can use a loop (upto 8) along with getch()/getchar() to get the user input

      Some things seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      1 Reply Last reply
      0
      • H hanlei0000000009

        char str[8];
        cin >> str;

        When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #3

        cin statement doesn't provide you with limited input support. since it wait for RETRUN/ENTER key for updating the input character in buffer supplied to it.!

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
        Never mind - my own stupidity is the source of every "problem" - Mixture

        cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>

        1 Reply Last reply
        0
        • H hanlei0000000009

          char str[8];
          cin >> str;

          When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!

          R Offline
          R Offline
          Rajkumar R
          wrote on last edited by
          #4

          std::cin.get(str, sizeof(str), '\n'); or std::cin >> std::setw(sizeof(str)) >>str;

          1 Reply Last reply
          0
          • H hanlei0000000009

            char str[8];
            cin >> str;

            When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!

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

            hanlei0000000009 wrote:

            When run, if inputed string size more than eight byte, result run time error.

            Were you expecting a different result? Why not use a string object instead?

            "Love people and use things, not love things and use people." - Unknown

            "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

            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