user1241357
user1241357

Reputation:

Why does the program loop infinitely?

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

Upvotes: 0

Views: 458

Answers (3)

Topo
Topo

Reputation: 5002

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

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

Upvotes: 1

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153792

The main lesson to take away from this is: always check after reading that the read was successful! It wasn't don't proceed. In general, input looks something like this:

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

... you'd use the input in the condition of a loop. Your main problems seem that the input for strings using in >> s first skips leading spaces and then reads non-space characters up to the first space (where space actually is any whitespace like space, newline, carriage return, form feed, backspace, etc.). If you want to read multiple words you'd need to determine how to best tell that reading should stop. For example you could read up to a specific character using std::getline(in, s, c) (where c defaults to \n if omitted).

If you try to read a value which can't be parsed successfully, e.g. when trying to read a number when the next non-space character isn't a digit, the stream will go into fail stated (i.e. its state gets the std::ios_base::failbit set) and it won't do anything useful until the state is clear()ed.

Upvotes: 1

Mahesh
Mahesh

Reputation: 34615

Use std::string

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

Upvotes: 0

Related Questions