0

I'm writing a program that needs to read user input into a string object. I'm using getline(cin, name), but when it gets to the prompt, I can type anything in and press enter, but it'll just go to the next line and the cursor will keep blinking, prompting for more input. Essentially, the prompt for input never seems to end, no matter how many characters I type or how many times I press enter. I don't even know if it's actually sending the input into the string object. What could be causing this?

This is the entirety of the main function so far (it's not complete, the switch-case will eventually have 6 options, but it compiles without error) The relevant section begins at switch case 1:

#include <string>
#include <array>
#include <iostream>
#include "stdlib.h"
#include "Bank.h"   //My own class. There is also a Bank.cpp, but I won't include the code in these unless they're deemed relevant

using namespace std;

void displayAccountInfo(); //will retrieve info on bank object

void main()
{
   int accsCreated = 0; //Keeps track of how many accounts have been created so far. Allows placement of pointer to new bank object at an empty array index.
   int option = 0;      //Stores the option chosen by the user. Used in switch-case. Also ends do-while loop when ==6.
   Bank* accounts[10];  //Will hold pointers to each bank object created by user in sequential order. No more than 10 accounts will ever be created.
   Bank* accpntr;       //Will point to one of the Bank objects. Used to initialize a pointer to the object in the accounts[] array.

   //begin Menu prompt
   do
   {
      cout << "[1] Create Bank object with values for accountNumber, name, and balance." << endl;
      cout << "[2] Create Bank object with no parameters." << endl;
      cout << "[6] End program" << endl;
      cin >> option;

      //begin option branch:
      switch (option)
      {
      case 1:
         {
            int num;             //holds account number
            string name;         //will hold account name as string object for use in Bank constructor
            double balance;      //holds account balance

            cout << endl << "Enter an account number: ";
               cin >> num;
            cout << endl << "Enter a name for the account: ";
               cin.ignore(std::numeric_limits<std::streamsize>::max()); //clears cin's buffer so getline() does not get skipped
               getline(cin,name);
            cout << endl << "Enter the balance of the account: ";
               cin >> balance;
            cout << endl;

            accpntr = new Bank(num, name, balance);   //creates a new bank object with attributes and a reference to it
            accounts[accsCreated] = accpntr; //adds pointer to new bank object to first empty spot in array
            accsCreated++;   //increments total of how many bank objects have been created/what index position of accounts[] to create the next one at
         }  break;   //end case 1
      case 2:
         {
            accpntr = new Bank();   //creates a new bank object with an accountNumber of 9999, an empty name attribute, and a balance value of zero.
            accounts[accsCreated] = accpntr; //adds pointer to new bank object to first empty spot in array
            accsCreated++;   //increments total of how many bank objects have been created/what index position of accounts[] to create the next one at
         }  break;   //end case 2

      case 6:
         {
            cout << "Ending Program." << endl;
         }
      }  //end switch-case block
   }
   while (option != 6); //end menu prompt when user chooses option 6.
      //end menu block
}  //end function main()
daphApple
  • 13
  • 1
  • 1
  • 3
  • possible duplicate of [cin and getline skipping input](http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input) – hivert Apr 06 '14 at 07:08
  • 1
    Nah, it's not a duplicate. That question has to do with needing to clear the buffer in the first place. I remembered to include the cin.ignore, but forgot to use the '\n' delimiter in it. Maybe this question'll help the next person who makes my silly mistake. – daphApple Apr 06 '14 at 07:27

2 Answers2

1

The first problem is here:

cin.ignore(std::numeric_limits<std::streamsize>::max());

istream::ignore behaves like other input functions. If there's no input in the buffer, it'll block and wait until it's avaliable. It'll stop once it ignores as many characters as you told it to, or once it reaches the delimiter character (the second parameter, which you didn't specify and it defaults to traits::eof). You never even reach getline call.

To fix it, specify the delimiter:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
jrok
  • 54,456
  • 9
  • 109
  • 141
  • *facepalm* Whoops. Can't believe I've been stuck on this for so long. This fixed it. Thank you so much. – daphApple Apr 06 '14 at 07:22
0

With alternating prompts and inputs, which impregnates a line structure on your input, you should stick to line-oriented input and read everything as a line, and use operations that convert numeric values from strings whenever appropriate.

This should be more robust than juggling with line delimiters.

laune
  • 31,114
  • 3
  • 29
  • 42