prenixd
prenixd

Reputation: 43

When i call a function twice the second time the function runs i can not input

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;

const int Length = 61;

void getinput (char []);


int main()
{
char a[Length];
char b[Length];

getinput(a);
getinput(b);


}

void getinput (char input[]){
    
    cout << "Enter Input: ";
    cin.get(input, 60);
    cout << "You Entered " << input <<endl;
    
}

When I run this code i can not input my second input it? I dont understand All i have done is just call the same function twice.

This is what gets output:

Enter Input: Hi

You Entered: Hi

Enter Input: You Entered

Upvotes: 0

Views: 197

Answers (1)

tdao
tdao

Reputation: 17713

That was because cin buffered input

cin.get(input, 60);

The second call would interpret the new line character left over from the first first call as its input.

To prevent such error, you may use getline instead:

std::cin.getline (input,60);

Upvotes: 2

Related Questions