简体   繁体   English

为什么这个循环不想正常工作?

[英]Why doesn't this loop want to work properly?

I've been at this for hours but I want to be able to add another diver and the only thing I need to show for it is the number of divers that were judged and their average score which I can do once this issue is fixed.我已经在这工作了几个小时,但我希望能够添加另一个潜水员,我唯一需要展示的是被判断的潜水员数量和他们的平均分数,一旦这个问题得到解决,我可以做这些。

It runs but when it loops around, it skips city, and eventually crashes the 2nd to 3rd time around.它运行但当它循环时,它跳过城市,并最终在第二到第三次崩溃。 Can anyone help?任何人都可以帮忙吗?

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

int main(){
    string name;
    string city;
    double judge[4];

    double total = 0;
    double score;
    int divers = 1;
    int x = 0;
    int y = 1;

    do{
        cout << "Please enter divers name: ";
        getline(cin, name);
        cout << "Enter the diver's city: ";
        getline(cin, city);

        do{
            cout << "Enter the score given by judge #" << y << ": " ;
            cin >> judge[x];

            total = total + judge[x];

            y++;
            x++;
        } while(y < 6);

        y = 1;

        cout << "Divers?";
        cin >> divers;

    } while(divers == 1);

    cout << city << endl;
    cout << name << endl;
    cout << total << endl;
    cout << judge[0] << endl;
    cout << judge[1] << endl;
    cout << judge[2] << endl;
    cout << judge[3] << endl;
    cout << judge[4] << endl;

    system("PAUSE");
}

索引从 0 开始,声明judge[4]意味着您将judge索引设为 0 1 2 3。您正在访问数组的末尾。

When you do cin >> divers;当你做cin >> divers; the end-of-line character is not removed from the input, just the number leading up to it.行尾字符不会从输入中删除,只是导致它的数字。 Then, the next time you ask for a line with std::getline() it returns just the end-of-line character that is already there and does not wait for your new input.然后,下次您使用std::getline()请求一行时,它只返回已经存在的行尾字符,而不会等待您的新输入。

So when you do cin >> drivers style input before a std::getline() style input you need to read past the end-of-line character.因此,当您std::getline()样式输入之前执行cin >> drivers样式输入时您需要读取行尾字符。

One way to do that is using the ignore() function:一种方法是使用ignore()函数:

do{
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "Please enter divers name: ";
    getline(cin, name);
    cout << "Enter the diver's city: ";
    getline(cin, city);

    // ...

Another way is to use the white-space eater std::ws in your std::getline() calls:另一种方法是在std::getline()调用中使用空白食者std::ws

do{
    cout << "Please enter divers name: ";
    getline(cin >> std::ws, name);
    cout << "Enter the diver's city: ";
    getline(cin >> std::ws, city);

    // ...

Strictly speaking only the first one is necessary.严格来说,只有第一个是必要的。 Remember the white-space eater will eat all the initial spaces you type into the getline() so you can't read in leading spaces if you use that technique.请记住,空格吞噬者会吃掉您在getline()键入的所有初始空格,因此如果您使用该技术,则无法读取前导空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM