在C ++中使用一个特定的键击退出while循环

时间:2016-11-07 16:58:32

标签: c++ while-loop

我正在尝试使用此特定字符键输入|退出while循环。 我想出如何在int中转换char用户输入的唯一方法。出于某种原因,当我在程序运行时输入|字符时,我只收到:Press any key to continue...。我的最终目标(我希望从我的代码中可以看到)是程序实际列出向量中的所有输入然后终止。

我是C ++的新手,在这一点上非常失败。如果有人能指出我正确的方向,我将非常感激。

我正在运行Windows 8 Entreprise计算机。

关于该计划

这是一个简单的程序,它接受用户输入的整数,将它们添加到矢量中并比较后两个以查看哪个更大,更小等。

完整代码在这里

#include "../../std_lib_facilities.h"

// conversion of units to meters

double funconvert(double x, string unit)
{
    const double cm_m = 1.0 / 100.0, in_cm = 2.54, ft_in = 12;
    if (unit == "m")
        return x;
    else if (unit == "cm")
        return x = x * cm_m;
    else if (unit == "in")
        return x = x * in_cm * cm_m;
    else if (unit == "ft")
        return x = x * ft_in * in_cm * cm_m;
    else
        cout << "Unknown unit value.\n";
}

// printing the large and the small variables
void printresult(double smallest, double largest)
{
    cout << "the smaller value is: " << smallest << "\n";
    cout << "the larger value is: " << largest << "\n";
}

int main() 
{
    vector<double>numbers;
    double a, b, smallest, largest;
    char the_stop;
    string unit;
    cout << "Please enter an intiger followed by a measurment unit [e.g 10cm]:";

    // Take input
    while (cin >> a)
    {
        char the_stop = a;
        cin >> unit;
        cout << the_stop;
        // check if this is the first number entered
        if (numbers.size() == 0)
        {
            a = funconvert(a, unit);
            b = a;
            numbers.push_back(a);
            numbers.push_back(b);
        }
        else if (the_stop == '|')
        {
            // This is not working for some reason... ERROR
            sort(numbers);
            for (int i = 0; i < numbers.size(); ++i)
                cout << numbers[i];
            break;
        }
        else
        {
            //assign a and b to the last two numbers in a vector
            a = funconvert(a, unit);
            numbers.push_back(a);
            a = numbers[(numbers.size() - 1)];
            b = numbers[(numbers.size() - 2)];
            // numbers.erase(numbers.begin()); // enable if desire to keep the vector empty
        }

        // find out which variable is larger and smaller
        if (a > b && (a/b-1) >= 0.01)
        {
            smallest = b;
            largest = a;
            printresult(smallest, largest);
        }
        else if (a < b && (b / a - 1) >= 0.01)
        {
            smallest = a;
            largest = b;
            printresult(smallest, largest);
        }
        else if ((a / b - 1) < 0.01 && (a / b - 1) != 0.00 || (b / a - 1) < 0.01 && (a / b - 1) != 0.00)
        {
            cout << "the nubers are almost equal\n";
        }
        else
            cout << a << " equals " << b << "\n";
    }
    return 0;
}

代码更新

#include "../../std_lib_facilities.h"

// conversion of units to meters

double funconvert(double x, string unit)
{
    const double cm_m = 1.0 / 100.0, in_cm = 2.54, ft_in = 12;
    if (unit == "m")
        return x;
    else if (unit == "cm")
        return x = x * cm_m;
    else if (unit == "in")
        return x = x * in_cm * cm_m;
    else if (unit == "ft")
        return x = x * ft_in * in_cm * cm_m;
    else
        cout << "Unknown unit value.\n";
}

// printing the large and the small variables
void printresult(double smallest, double largest)
{
    cout << "the smaller value is: " << smallest << "\n";
    cout << "the larger value is: " << largest << "\n";
}

int main() 
{
    vector<double>numbers;
    double a, b, smallest, largest;
    char the_stop;
    string unit;
    cout << "Please enter an intiger:";

    // Take input
    while (cin >> a)
    {

        // check if the | character is pressed
        char the_stop = a;
        if (the_stop == '|')
        {
            cout << "start";
            sort(numbers);
            for (int i = 0; i < numbers.size(); ++i)
                cout << numbers[i];
        }
        else
        {
            cout << "And now the unit value:";
            cin >> unit;
        }

        // check if this is the first number entered
        if (numbers.size() == 0)
        {
            a = funconvert(a, unit);
            b = a;
            numbers.push_back(a);
            numbers.push_back(b);
        }
        else
        {
            //assign a and b to the last two numbers in a vector
            a = funconvert(a, unit);
            numbers.push_back(a);
            a = numbers[(numbers.size() - 1)];
            b = numbers[(numbers.size() - 2)];
            // numbers.erase(numbers.begin()); // enable if desire to keep the vector empty
        }

        // find out which variable is larger and smaller
        if (a > b && (a/b-1) >= 0.01)
        {
            smallest = b;
            largest = a;
            printresult(smallest, largest);
        }
        else if (a < b && (b / a - 1) >= 0.01)
        {
            smallest = a;
            largest = b;
            printresult(smallest, largest);
        }
        else if ((a / b - 1) < 0.01 && (a / b - 1) != 0.00 || (b / a - 1) < 0.01 && (a / b - 1) != 0.00)
        {
            cout << "the nubers are almost equal\n";
        }
        else
            cout << a << " equals " << b << "\n";
    }
    return 0;
}

0 个答案:

没有答案