Why does cin not run normally on c++?

Asked 2 years ago, Updated 2 years ago, 25 views

cin을 사용하여 입력할 이름의 개수와 이름을 입력하고 이를 출력하는 간단한 코드 입니다.

But if you add the cin >> n part in the third line of main, Contrary to my intentions, the next for statement has a single sin It's not accepted.

Why are you doing that?

If sin>>n does not receive the number of names to enter, it will function normally.

using namespace std;

int main() { int n = 5; cout << "How many names do you want to enter?" <<endl; cin >> n;

string *names = new string[n];
for (int i = 0; i < n; i++) {
    cout << "Name >> ";
    getline(cin, names[i], '\n');
}

string latter = names[0];
for (int i = 1; i < n; i++) {
    if (latter < names[i]) {
        latter = names[i];
    }
}

for (int i = 0; i < n; i++) {
    cout << names[i] << endl;
}

}

c++ cin

2022-09-22 20:36

1 Answers

It's been a while since I played c++, so I'm not sure If the value is set to sin>>, the opening value remains after entering the value I understand that it affects the next getline(). sin>>n; then sin.Try adding ignore()


2022-09-22 20:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.