cin and cin.ignore() doesn't work properly

73 views Asked by At

I have trouble with cin and cin.ignore(). This is my code and it doesn't have any error but it doesn't work properly. when I want to see the ID results it shows me something weird.

#include <iostream>
#include <cstring>

using namespace std;

class watchTower {
private:
    int numberOfAirplane;
    int runway;
    int numberOfOrders;
    string menu;
    char ID[][11];


public:
    void setNumberOfAirplane(int n) { numberOfAirplane = n;}
    void setRunWay(int k) { runway = k;}
    void setID(char id[][11]);
    void setNumberOfOrders(int q) { numberOfOrders = q;}
    void display();
};


void watchTower::display() {
    cout << numberOfAirplane << ' ' << runway << ' ' << numberOfOrders << endl;
    for(int i = 0; i < numberOfAirplane; i++)
        cout << ID[i] << endl;

}

void watchTower::setID(char (*id)[11]) {
    for(int i = 0; i < numberOfAirplane; i++)
        for(int j = 0; j < 11; j++)
            ID[i][j] = id[i][j];
}



int main() {
    int n, k, q;
    cin >> n >> k;
    char id[n][11];
    for(int i = 0; i < n; i++) {
        cin.ignore(256, '\n');
        cin >> id[i];
        cin.clear();

    }
    watchTower input;
    input.setNumberOfAirplane(n);
    input.setRunWay(k);
    input.setID(id);
    cin >> q;
    input.setNumberOfOrders(q);
    input.display();
}

As you can see I tried both cin.ignore and cin.clear but they don't work. by the way these are my inputs.

3 4 0000000001 0000000002 0000000003 5

and these are my outputs which are incorrect.

3 4 5 0000000001 0♣ 0000000003

0

There are 0 answers