为什么在句子末尾按 Enter 时结果不同?

Why the result differs when I press enter at the end of a sentence?

这是我的第一个程序

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int a;
    string s;
    double d;
    while(cin >> a >> s >> d)
        cout << a << s << d;
    return 0;
}

当我输入一些简单的数据,然后按输入,结果立即显示:

但是,另一个程序中的代码表现不同:

#include <iostream>
#include <string>
using namespace std;

struct Sales_data {
    string bookNo;
    unsigned units_sold = 0;
    double price = 0.0;
    void Print();
};

void Sales_data::Print(){//print every record of the Sales_data
    cout << "The bookNo of the book is " << bookNo << endl;
    cout << "The units_sold of the book is " << units_sold << endl;
    cout << "The price of the book is " << price << endl;
}

int main()
{
    Sales_data book;
    while(cin >> book.bookNo >> book.units_sold >> book.price);
        book.Print();
    return 0;
}

当我运行这段代码,输入一些数据,然后按Enter,它等待我输入更多数据而不是显示结果。

你能给我解释一下吗?

去掉while循环后的分号。事实上,它强制循环没有主体,这意味着它只是在 cin 上永远循环。更好的是,使用大括号来分隔正文:

while(cin >> book.bookNo >> book.units_sold >> book.price) {
    book.Print();
}