C++ EOF 帮助使用 Ctrl + Z 和 While 循环

问题描述 投票:0回答:1

我有点难以理解如何正确设置它。

只是一些背景知识。我是编程新手,现在才使用 C++ 3 周。

我们遇到一个问题,允许用户使用 EOF 控制的 while 循环输入任意数量的信息来迭代多个条目。

虽然我能够为此设置通用代码,但当我尝试使用 eof 函数 ctrl+z 退出循环时,我遇到了循环的最后输出仍在打印的问题。

我设置了代码来在进入循环之前预读取用户输入的数据,但是当我启动 ctrl + z 时,它会退出循环,但仍然读取到循环末尾并打印请求数据的输出:

输入目的地名称:(此处ctrl+z使用eof退出循环)

输入行驶的英里数:输入使用的加仑数:(打印其余输出)

如何使用 ctrl + z 停止输出最后一部分?

任何帮助或意见表示赞赏

这就是我设置初始代码的方式:


#include <iostream>
#include <string>

using namespace std;

int main()
{
    //define variables
    double milesTravelled, gallonsUsed, mpg, total_miles, 
    avg_mpgPerTrip;
    int no_of_trips;
    string destinationName;

    //set counter and sum to 0
    no_of_trips = 0;
    total_miles = 0;

    //input phase
    cout << "Enter the name of the destination: ";
    cin >> destinationName;

    cout << "Enter the number of miles travelled: ";
    cin >> milesTravelled;

    cout << "Enter the number of gallons used: ";
    cin >> gallonsUsed;

    //process phase
    while(cin)//while loop EOF
    {
        mpg = milesTravelled / gallonsUsed;

        //update counter and sum
        total_miles += milesTravelled;
        no_of_trips ++;

        //output phase
        cout << "Travel Information: \n"
            << endl
            << "Destination: " << destinationName << endl
            << "Miles per Gallon: " << mpg << endl
            << endl;

        //loop exit exit prompt
        cout << "To exit the program, press: ctrl + Z, then Enter\n"
            << endl;

        //input next line of data
        cout << "Enter the name of the destination: ";
        cin >> destinationName;

        cout << "Enter the number of miles travelled: ";
        cin >> milesTravelled;

        cout << "Enter the number of gallons used: ";
        cin >> gallonsUsed;
        cout << endl;

    }//end while

    //output of counter and sum
    cout << endl
        << "Trip information entered:" << endl
        << endl
        << "Total number of trips taken: " << no_of_trips << 
        endl
        << "Total number of miles travelled: " << total_miles 
        << endl;

    return 0;
}

我尝试使用使用 if, else 的控制结构来告诉程序根据 EOF ctrl + z 退出

c++ while-loop eof
1个回答
0
投票

while
循环将在每次循环迭代开始时检查循环条件。如果您希望在循环中间而不是在循环开始或结束时检查循环条件,那么我建议使用带有显式 break 语句的无限循环。

对于您的情况,我建议您将

while
循环替换为以下代码:

for (;;) // infinite loop ( equivalent to "while (true)" )
{
    mpg = milesTravelled / gallonsUsed;

    //update counter and sum
    total_miles += milesTravelled;
    no_of_trips ++;

    //output phase
    cout << "Travel Information: \n"
        << endl
        << "Destination: " << destinationName << endl
        << "Miles per Gallon: " << mpg << endl
        << endl;

    //loop exit exit prompt
    cout << "To exit the program, press: ctrl + Z, then Enter\n"
        << endl;

    //input next line of data
    cout << "Enter the name of the destination: ";
    cin >> destinationName;
    if ( cin.fail() )
        break;

    cout << "Enter the number of miles travelled: ";
    cin >> milesTravelled;
    if ( cin.fail() )
        break;

    cout << "Enter the number of gallons used: ";
    cin >> gallonsUsed;
    if ( cin.fail() )
        break;

    cout << endl;
}
© www.soinside.com 2019 - 2024. All rights reserved.