0

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");
}
4
  • 6
    Just paste your code into the question (you can use edit to get back to it). Highlight, and click the "{}" button to code format it. You will see it in the preview below to see if it looks right. Commented Mar 8, 2012 at 0:30
  • Sorry - we'll need at least some source code to comment on what is wrong with your current code. As to how to put it here, copy and paste it, with four spaces before each line, or use pastebin Commented Mar 8, 2012 at 0:31
  • 3
    @lochok: Don't link to pastebin from here. Commented Mar 8, 2012 at 0:32
  • cin just saves the first word into name. you need to use getline. Commented Mar 8, 2012 at 0:40

3 Answers 3

1

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");
}
Sign up to request clarification or add additional context in comments.

4 Comments

Wow! Thanks! why people disliked this post so much?
@Napster The post is a little to newbie, plus at first you didn't provide any code so there was no way to help you.
@Topo, did you run your program? It leaves the line-terminating '\n' after the loop, so the next loop's getline fails.
@Rob You are right, that's because 'cin' leaves the '\n' in the stream. I think he should replace all instances of 'cin' with 'getline' and use a stringstream to get the numbers or ask for the name and last name without spaces or in different cin commands. But the safer way is to use the getline always to ensure that the input is of the desired type and avoid infinite loops.
1

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.

Comments

0

Use std::string

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.