aftab
aftab

Reputation: 1

Can't input two char array in c++ continuously

It is skipping the second array to take input.

#include <iostream>
using namespace std;

int main()
{
    char a[5],b[5];
    cout<<"Enter a : ";
    cin.get(a,5)
    cout<<"Enter b : ";
    cin.get(b,5);
    cout<<"Done";
    return 0;
}

Output:

Enter a : "abc"
Enter b :
Done

Upvotes: 0

Views: 529

Answers (1)

Marek R
Marek R

Reputation: 38062

Here is minimal complete verifiable example which reproduces your issue.

#include <iostream>

using namespace std;

int main()
{
    char a[5], b[5];

    cin.get(a, 5);
    cin.get(b, 5);

    cout << "a = " << a << '\n';
    cout << "b = " << b << '\n';

    return 0;
}

https://wandbox.org/permlink/NonDLZiDhaIyKff4

Now you should start from reading documentation:

std::basic_istream::get - cppreference.com

  • the next available input character c equals delim, as determined by Traits::eq(c, delim). This character is not extracted (unlike basic_istream::getline())

so problem is delimiter.

It would be best if you write code which is more C++ and use such code (not like C code):

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a, b;

    std::getline(cin, a);
    std::getline(cin, b);

    cout << "a = " << a << '\n';
    cout << "b = " << b << '\n';

    return 0;
}

https://wandbox.org/permlink/iWcHo2jpVHKdnIJy

Upvotes: 1

Related Questions