【问题标题】:How to re-input a variable outside of a loop如何在循环外重新输入变量
【发布时间】:2019-03-15 00:47:33
【问题描述】:

因此,当输入变量之一出现错误时,我的程序中的所有内容似乎都运行良好。

Ex. 总工资。我需要重新输入员工编号,但它一直给出一开始就已经给出的那个。我不知道如何使重新输入员工编号而不是已经输出成为可能。我只能使用循环和 if 语句。我不会做数组。

请查看以下代码

{
const int QUIT = 0, NO_NEG = 0;
int empNum = 1;
double grossPay, fedWith,
    stateWith, ficaWith, sum,
    totalGross = 0, totalFed = 0,
    totalState = 0, totalFica = 0,
    netPay, totalNetPay = 0;


cout << "Enter the following information:\n\n"
    << "Employee Number (0 to quit): ";
cin >> empNum;

while (empNum != QUIT)
{
    do
    {
        cout << "Gross pay: ";
        cin >> grossPay;

        while (grossPay < NO_NEG)
        {
            cout << "Gross pay may not be less than zero.\n";
            cout << "Re-enter Gross pay: ";
            cin >> grossPay;
        }

        cout << "Federal Withholding: ";
        cin >> fedWith;

        while (fedWith < NO_NEG)
        {
            cout << "Federal witholding may not "
                << "be less than zero.\n";
            cout << "Re-enter Federal Withholding: ";
            cin >> fedWith;
        }

        cout << "State Withholding: ";
        cin >> stateWith;

        while (stateWith < NO_NEG)
        {
            cout << "State witholding may not "
                << "be less than zero.\n";
            cout << "Re-enter State Withholding: ";
            cin >> stateWith;
        }

        cout << "FICA Withholding: ";
        cin >> ficaWith;

        while (ficaWith < NO_NEG)
        {
            cout << "FICA witholding may not be less than zero.\n";
            cout << "Re-enter FICA Withholding: ";
            cin >> ficaWith;
        }

        sum = (stateWith + fedWith + ficaWith);

        if (sum > grossPay)
        {
            cout << "\nERROR: Withholdings cannot"
                << " exceed gross pay.\n"
                << "\nPlease re-enter the data for this employee.\n"
                << "Processing the next employee:\n"
                << "\nEmployee Number (0 to quit): "
                << empNum << endl;
        }

        cout << "Processing the next employee:\n";

    } while (sum > grossPay);

    totalGross += grossPay;
    totalFed += fedWith;
    totalState += stateWith;
    totalFica += ficaWith;

    netPay = grossPay - sum;

    totalNetPay += netPay;

    cout << "Employee Number (0 to quit): ";
    cin >> empNum;

}

cout << "Total Gross Pay: $ " << totalGross << endl
    << "Total Federal Tax: $ " << totalFed << endl
    << "Total State Tax: $ " << totalState << endl
    << "Total FICA: $ " << totalFica << endl
    << "Total Net Pay: $ " << totalNetPay << endl;

return 0;
}

【问题讨论】:

  • 你调试代码了吗?
  • 想想您正在打印什么以及何时打印。你继续打印"Processing the next employee",而这不是代码正在做的事情。当程序不期望任何输入时,您还要求输入。
  • 是的,我做到了。我尝试了不同的方式,但我被卡住了。我在编码方面还很新,所以我不知道解决方案是否简单

标签: c++ loops variables while-loop


【解决方案1】:

您已经在底部重新输入。当你问:

cout << "Employee Number (0 to quit): ";
cin >> empNum;

我的建议是使用 getline(cin, string_name)。将您的 empNum 变量切换为 C 字符串。它是一种数组,但它也是一个字符串。

将您的第一个 cout 和 cin 语句从第一个 while 循环上方移动到 do-while 循环内部。像这样:

 while (empNum != "0")
 { 
     do { 
     cout << "Enter the following information:\n\n"
     << "Employee Number (0 to quit): ";
     getline(cin, empNum);

另外,从

重新分配变量 empNum
 int empNum = 1;

 string empNum;

还有,下边:

 cout << "Processing the next employee:\n";

将这两行放在它下面,它会清除缓冲区,我假设这是你在重新输入变量时想要做的。

 cin.clear();
 cin.ignore();

除此之外,重新输入变量的最简单方法就是请求它们。此外,您是否在谈论在输入器检查您的虚假信息之一后重新输入变量。如果是这样,您正在为已输入的错误员工数据重新输入数据。祝你的程序好运!

虽然我发现字符串比较存在一些讨厌的问题,但您可以使用这个 ^^^。 const char 和 chars 不能比较 字符串不能和 chars 比较,所以你会得到很多错误。

您的描述说您需要重新输入变量,而不是已经输出。 empNum 变量被重新输入,只要:

 (empNum != to QUIT)

是真的。唯一一次重新输入变量是您的员工数据不正确。

【讨论】:

  • 这行得通,但是现在每当我输入 0 时,程序都会继续循环。我不知道如何创建一个循环,当输入 0 时它会停止循环。
  • 问题是我们在第一个while循环中比较一个整数和一个字符串。所以你需要做的是使用 strstr/strcmp 来比较字符串。
猜你喜欢
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多