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. cin causes infinite loop in my program

cin causes infinite loop in my program

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
6 Posts 6 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.
  • S Offline
    S Offline
    Spherelin
    wrote on last edited by
    #1

    I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G

    _ P L D M 5 Replies Last reply
    0
    • S Spherelin

      I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G

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

      Its probably because in the first getline the BUFFER is too small or the user is entering a string whose length in bigger than BUFFER.

      « Superman »

      1 Reply Last reply
      0
      • S Spherelin

        I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #3

        I think. In this case, it will go in to loop for characters only. Might be problem with overload of operator >> with int parameter. It is not handling char properly.

        Prasad Notifier using ATL | Operator new[],delete[][^]

        1 Reply Last reply
        0
        • S Spherelin

          I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Hello, i have an question to, why dont use cin directly like this: cin >> tempstring; // :confused: with best regards break;

          1 Reply Last reply
          0
          • S Spherelin

            I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G

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

            Spherelin wrote:

            cin.getline(tempstringgoeshere, BUFFER);

            What is the value of BUFFER and how many characters are you entering? You'll quickly find that tempstringgoeshere is not large enough. Use a string type instead of a char type.


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            1 Reply Last reply
            0
            • S Spherelin

              I am writing this program where I read in a line of text. I am able to enter the text just fine and it stores okay, but then when I go to prompt user for another entry (such as an integer) I get an infinite loop because it is waiting for a valid entry. example: cout << "please enter a string of characters."; cin.getline(tempstringgoeshere, BUFFER); int selection = 0; while(selection < 1 || selection > 5) { cout << "Please make a selection:"; cin >> selection; // DOES NOT LET ME ENTER ANYTHING HERE - GOES PAST } Suggestions? Thanks, G

              M Offline
              M Offline
              MyFathersSon
              wrote on last edited by
              #6

              Unless your input is an array of strings then use cin>>tempstring otherwise use cin.getline(tempstring, MAXBUFFERSIZE, '\n'); this function takes 3 parameters, and the last one is the delimiter i.e. when to signal the function to return.

              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