我想知道當我執行cin.get()時如何擺脫'\ n'。例如:如何終止' n'爲std :: cin.get()
#include <iostream>
int main() {
std::cout << "How many pieces of c: ";
struct c{
std::string name;
int val;
};
int amount;
std::cin >> amount;
c * c1 = new c[amount];
std::cin.get();
for(int i = 0; i < amount; i++){
std::cout << "C Number " << i + 1 << ":" << std::endl;
std::cout << "Please enter the name: ";
c1[i].name = std::cin.get();
std::cout << "Please enter the val: ";
std::cin >> c1[i].val;
std::cin.get();
}
return 0;
}
我使用cin.get(),以消除「\ n」我給std :: cin後這就是最後一次讀取嘗試過,但我的程序只需要在第一個名稱,然後執行程序的其餘部分不讓我繼續輸入名稱和val值的其餘部分。總之,我想能夠存儲我的名字和val值的其餘部分。謝謝!
請參見[this](https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces)和[this](https://stackoverflow.com/questions/21567291/why-does-stdgetline -skip-輸入後一個格式化的提取) – NathanOliver