试图走出这个循环

时间:2013-06-10 02:56:11

标签: c++

如果代码看起来很草率,我很抱歉。我只学了一些循环,如果到目前为止书中的检查。我想编码走出循环。我尝试过使用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();
}

1 个答案:

答案 0 :(得分:3)

在循环之前检查leave条件,这当然不会很好。你应该在里面循环检查它。

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

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

您似乎希望输入 数字和字符串,只是一个字符。这不起作用,因为变量xdouble并且无法处理输入的字符串或字符。您实际上应该收到关于使用double作为字符的警告(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(我猜这是读取字符串或字符)将得到{ {1}}并立即退出。