2 Replies - 205 Views - Last Post: 05 February 2018 - 04:51 PM Rate Topic: -----

#1 CVRIV  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 03-February 18

Problem with std::cin input buffer

Posted 05 February 2018 - 03:45 PM

Here's the code:
// GLOBAL DECLARATIONS
char GBL_YesNo;

// MAIN
int main(){
   string P4_Name, P4_Addr, P4_City, P4_State, P4_Zip; 

   // GATHER INPUT FROM THE USER
   do{
      // GATHER NAME
      std::cout << "\nWhat is your full name?" << endl;
      getline(std::cin, P4_Name);
      std::cout << P4_Name << endl;
			
      // GATHER ADDRESS
      std::cout << "\nWhat is your address?" << endl;
      getline(std::cin, P4_Addr);
      std::cout << P4_Addr << endl;
			
      // GATHER CITY
      std::cout << "\nWhat city do you live in??" << endl;
      getline(std::cin, P4_City);
      std::cout << P4_City << endl;
			
      // GATHER STATE
      std::cout << "\nWhat state do you live in??" << endl;
      getline(std::cin, P4_State);
      std::cout << P4_State << endl;
			
      // GATHER ZIP
      std::cout << "\nWhat is your zip code?" << endl;
      getline(std::cin, P4_Zip);
      std::cout << P4_Zip << endl;

      // DISPLAY THE INPUT
      std::cout << "\nHere is the information you entered:"
                << "\n\n" << P4_Name
                << "\n" << P4_Addr
                << "\n" << P4_City << ", " << P4_State << ", " << P4_Zip << endl;
			
      // ASK IF THE INPUT IS CORRECT
      do{
         std::cout << "\nIs this correct? [Y]es or [N]o?" << endl;
         std::cin >> GBL_YesNo;
      }while(GBL_YesNo != 'Y' && GBL_YesNo != 'y' && GBL_YesNo != 'N' && GBL_YesNo != 'n'); // Only possible keys (Y, y, N, n).
   }while(GBL_YesNo != 'Y' && GBL_YesNo != 'y');

   std::cout << "\nExcellent. Thank you for verifying your information.";
}



Here's the output:
What is your full name?
c r
c r

What is your address?
m a
m a

What city do you live in??
n p
n p

What state do you live in??
n j
n j

What is your zip code?
0
0

Here is the information you entered:

c r
m a
n p, n j, 0

Is this correct? [Y]es or [N]o?
n

What is your full name? 


What is your address?
m a
m a

What city do you live in??
n p
n p

What state do you live in??
n j
n j

What is your zip code?
0
0

Here is the information you entered:


m a
n p, n j, 0

Is this correct? [Y]es or [N]o?
y

Excellent. Thank you for verifying your information.



The problem is when the user enters all of the information and then specifies that it's not correct. When it loops back to the beginning to acquire the information again, it skips the first input. It writes to std::cout but skips the getline(). I have no idea why it's doing this.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with std::cin input buffer

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5207
  • View blog
  • Posts: 13,953
  • Joined: 18-April 07

Re: Problem with std::cin input buffer

Posted 05 February 2018 - 04:06 PM

Keep in mind that when you use cin, it is going to leave a new line / carriage return in the input buffer. So when you come back around and do a getline() it is going to eat that carriage return as the input and continue on, not giving the user the chance to enter anything new.

You can read about it and some tricks to help you deal with the problem using the link below...

http://www.augustcou...s.html#problems

Hope this helps. :)
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Problem with std::cin input buffer

Posted 05 February 2018 - 04:51 PM

Renamed topic title to be more descriptive of problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1