[英]C++ simple code issue about cout and cin
我有以下代碼。 一切都很好,但完全不同。 我希望前三個cout <<
一個接一個地來,所以當第一個消息顯示在控制台中時,用戶輸入值,然后下一個cout <<
顯示另一個消息,用戶輸入該書的名稱,然后第三個cout <<
顯示最后一條消息,用戶輸入年份。 但是它顯示了第一條消息,我輸入了值,然后它顯示了接下來的兩條消息。 為什么?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string AuthorName;
string AuthorBook;
string YearPublished;
cout << "Please Enter the Author Name" << endl;
cin >> AuthorName;
cout << "Please enter the Author Book" << endl;
cin >> AuthorBook;
cout << "Please enter the year when the book was published" << endl;
cin >> YearPublished;
cout << setw(15) << "Author Name";
cout << setw(15) << "Prominent Work";
cout << setw(15) << "Year Published";
cout << endl << endl;
cout << setw(15) << AuthorName;
cout << setw(15) << AuthorBook;
cout << setw(15) << YearPublished;
cout << endl << endl;
return 0;
}
您需要使用getline()
因為C ++使用cin
停止在字符串的第一個空格處讀取輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.