为什么程序无限循环?

时间:2012-03-08 00:29:25

标签: c++

如何输入像“彼得约翰逊”这样的字符串? 我的程序只读取1个单一名称,如果我放置一个空格,程序无限循环 写约翰,作品,但空间角色使它循环。这是为什么?我也知道程序可能没有完全完成。

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
   int x=0;
   char name [25];
   float paycheck;

   cout<<"WELCOME TO THE EMPLOYEE SALARY GENERATION PROGRAM\n\n";

   while (x!=-1)
   {
      cout<<"Enter employee name or -1 to stop the program\n";
      cin>>name;
      cout<<"Enter employee code (1=manager, 2=worker, 3=comission, 4=pieceworker) or -1            to stop the program\n";
      cin>>x;

      switch (x)
      {
        case 1:

           cout<<"Your weekly total paycheck is 2 500 $\n";  // FIXED weekly manager's     salary
           break;

        case 2:  // 8.50 per hour + over time for workers
           cout<<"Please enter the amount of hours worked\n";
           cin>>paycheck;
           if(paycheck<40)
                   paycheck=paycheck*8.50;
           else
                   paycheck= (paycheck-40)*8.50 +(40*8.50);
           cout<<name<<"'s paycheck is "<<paycheck<<"$\n";
           break;

        case 3:  // comission workers make 250 + 5.7% of their weekly sales
           cout<<"Please enter amount of weekly sale made\n";
           cin>>paycheck;
           paycheck = paycheck*5.7/100 + 250;
           break;

        case 4: // pieceworkers make 50$ per item produced
           cout<<"Please enter the number of items produced this week\n";
           cin>>paycheck;
           paycheck = paycheck*50;
           cout<<"The employee"<<name<<"Made"<<paycheck<<"$ this week";
           break;

        default:
           break;
      }
   }

   system ("PAUSE");
}

3 个答案:

答案 0 :(得分:1)

'cin'函数在找到空格时停止读取。使用'getline'来读取名称。

编辑:调试代码,并添加一些安全措施,以避免因输入错误导致程序崩溃。

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

float foo()
{
    float fl = 0.0f;
    string str;
    while(true) {
        getline(cin, str);
        stringstream sstream(str);
        if (sstream >> fl)
            break;
        cout << "Invalid Input" << endl;
    }
    return fl;
}

int main()

{
    string x;
    string name;
    char number = {0};
    float paycheck;

    cout << "WELCOME TO THE EMPLOYEE SALARY GENERATION PROGRAM" << endl << endl;

    while (x!="-1") {
        cout << "Enter employee name or -1 to stop the program" << endl;
        getline(cin, name);
        if (name == "-1") return 0;

        cout<<"Enter employee code (1=manager, 2=worker, 3=comission, 4=pieceworker) or -1 to stop the program\n";
        getline(cin, x);
        if (x == "-1") return 0;
        if (x.length() == 1)
            number = x[0];
        else {
            cout << "Invalid Input" << endl;
            continue;
        }

        switch (number) {
        case '1':
            cout << "Your weekly total paycheck is 2 500 $" << endl;  // FIXED weekly manager's     salary
            break;
        case '2':  // 8.50 per hour + over time for workers
            cout << "Please enter the amount of hours worked" << endl;
            paycheck = foo();
            if(paycheck<40)
                paycheck=paycheck*8.50;
            else
                paycheck= (paycheck-40)*8.50 +(40*8.50);
            cout << name << "'s paycheck is " << paycheck << "$"  << endl;
            break;
        case '3':  // comission workers make 250 + 5.7% of their weekly sales
            cout << "Please enter amount of weekly sale made" << endl;
            paycheck = foo();
            paycheck = paycheck*5.7/100 + 250;
            break;
        case '4': // pieceworkers make 50$ per item produced
            cout<<"Please enter the number of items produced this week" << endl;
            paycheck = foo();
            paycheck = paycheck*50;
            cout<<"The employee " << name << " Made "<< paycheck << "$ this week" << endl;
            break;
        default:
            cout << "Invalid Option." << endl;
            break;
        }
    }
    system ("PAUSE");
}

答案 1 :(得分:1)

主要教训是:始终在阅读读取成功后进行检查!这不是不要继续。通常,输入看起来像这样:

if (in >> var1 >> var2) { ... }
if (std::getline(in, string)) { ... }

...你在循环条件下使用输入。您的主要问题似乎是使用in >> s的字符串输入首先跳过前导空格,然后读取非空格字符直到第一个空格(其中空间实际上是任何空格,如空格,换行符,回车符,换页符,退格键)等)。如果你想阅读多个单词,你需要确定如何最好地告诉你应该停止阅读。例如,您可以使用std::getline(in, s, c)读取特定字符(如果省略,则c默认为\n。)

如果您尝试读取无法成功解析的值,例如当下一个非空格字符不是数字时尝试读取数字时,流将进入失败状态(即其状态获得std::ios_base::failbit设置)并且在状态之前它将不会执行任何有用的操作是clear()编辑。

答案 2 :(得分:0)

使用std::string

std::string myName;
std::getline(std::cin, myName);