关于cout和cin的c++简单代码问题
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
在字符串的第一个空格处停止读取您的输入。
相关文章:
- boost::spirit--试图编译大多数简单代码的编译器错误
- 奇怪的 G++ 错误,带有关于左值和赋值的简单代码
- 为什么 for 循环只接受这个简单代码中的最后一个字符串?
- SFML C 为什么在屏幕上没有绘制?(简单代码)
- GoogleMock发出用Eclipse构建C 的简单代码
- 为什么我的简单代码在C++中不起作用?
- PQXX简单代码崩溃了
- 此简单代码的正统代码
- 我在C++文件中的简单代码块上有 2 个错误。错误 C2059:语法错误:"?"。第二智能感知:预期表达式。怎么了?
- 用谷歌基准测试简单代码的问题
- 简单代码给出C 错误C4430和C2143
- 这个带有向量的简单代码有什么问题?
- 优化项目Euler 16的简单代码
- 如何重写此 C++ 简单代码
- 是否有Visual c++编译器在线,以及如何在c++和vs简单代码之间转换
- 简单代码问题-未定义标识符
- OpenCV简单代码编译成功,但在运行时崩溃
- 如何在另一个类中构造一个类?(简单代码)
- 验证给定C++编译器是否将源代码读取为 C++11 的快速简单代码是什么?
- 带有类的 C++ 简单代码