为什么程序无限循环? [英] Why does the program loop infinitely?

查看:169
本文介绍了为什么程序无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何输入一串字符串,例如Peter Johnson?
我的程序只读取一个单一的名称,如果我放一个空格,程序无限循环
写约翰,工作,但空格字符使它循环。这是为什么?此外,我知道程序可能无法完全完成。

How to input a string of charaters like "Peter Johnson"? My program only reads 1 single name, if I put a space, the program loops infinitely Writting John, works, but the space character makes it loop. Why is that? Also I know the program might not be completely finished.

#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");
}


推荐答案

当它找到一个空间。使用'getline'读取名称。

The 'cin' function stops reading when it finds a space. Use 'getline' to read the names.

编辑:调试代码,并添加了一些安全措施,以避免程序崩溃, >

Debug the code, and added some safe measures to avoid program crashing due to bad input.

#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");
}

这篇关于为什么程序无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆