0

如果代码看起来很草率,我很抱歉。到目前为止,我只在书中学习了一些循环和 if 检查。我想编写代码以跳出循环。我试过使用break,然后我尝试使用这个循环。

#include "std_lib_facilities.h"

using namespace std;

int main()
{
    double smallest = 0;
    double largest = 0;
    double x = 0;
    string measure = " ";
    double sum = 0;
    vector<double> meters;
    bool leave = false;

    if (!leave)
    {
    while(cin>>x>>measure)
    {

        if (x < largest)
            smallest = x;
        else
            largest = x;

        if ( x == 'x')
            leave = true;

        cout << "You typed " << x << " Smallest number so far: " << smallest << " Largest number so far: " << largest << endl;

        if(measure == "in") //inches
        {
            cout << "You wanted inches? : " << x << " inches" << endl;
            cout << "That also happens to be : " << x * 2.54 << " cm" << endl; // inches to cm
            sum += x * 0.0254;
            meters.push_back(sum);
            cout << "Meters so far : " << sum << endl;
        }
        else if(measure == "cm") //centimeter
        {
            cout << "You wanted centimeters? : " << x << " centimeter" << endl;
            cout << "That also happens to be : " << x / 100 << " m" << endl; // inches to cm
            sum += x / 100;
            meters.push_back(sum);
            cout << "Meters so far : " << sum << endl;
        }
        else if(measure == "f") //feet
        {
            cout << "You wanted feet? : " << x << " feet" << endl;
            cout << "That also happens to be : " << x * 12 << " inches" << endl; // inches to cm
            sum += x * 0.3048;
            meters.push_back(sum);
            cout << "Meters so far : " << sum << endl;
        }
        else if(measure == "m") //meters
        {
            cout << "You wanted meters? : " << x << " meters" << endl;
            cout << "That also happens to be : " << x * 100 << " cm" << endl; // inches to cm
            sum += x;
            meters.push_back(sum);
            cout << "Meters so far : " << sum << endl;
        }

        else
        {
            cout << "error invalid measurement. " << endl;
            keep_window_open();
        }
    }
    }

    for(int i = 0; i<meters.size(); ++i)
        cout << meters[i];


    keep_window_open();
}
4

1 回答 1

3

您在循环之前检查leave条件,这当然不会很好地工作。您应该在循环内检查它。

它可以最简单地放入实际的循环条件中:

while(!leave && cin >> x >> measure) { ... }

似乎您希望输入是数字和字符串,或者只是一个字符。这将不起作用,因为变量x是 adouble并且无法处理正在输入的字符串或字符。您实际上应该收到有关使用 adouble作为字符的警告(x == 'x'比较)。

做类似的事情可能会更好

std::string input;
while (std::getline(std::cin, input))
{
    std::istringstream is(input);

    // Try to get a number and a string
    if (is >> x >> measure)
    {
        // Got it, do stuff here...
    }
    else
    {
        // Input was not a number and a string, try to get a character
        char ch;
        if (is >> ch && ch == 'x')
            break;  // Exit loop
        else
        {
            std::cout << "Wrong input, have to be a number and a string, or 'x' to exit\n";
        }
    }
}

您在程序突然退出时遇到的问题很可能是因为这个。当输入语句std::cin >> x >> measure无法将输入读取为数字时,它会将字符留在输入缓冲区中,因此keep_window_open(我猜它读取字符串或字符)将得到它'x'并立即退出。

于 2013-06-10T03:01:07.763 回答