Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7600317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:52:53+00:00 2026-05-30T22:52:53+00:00

How to input a string of charaters like Peter Johnson? My program only reads

  • 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");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:52:54+00:00Added an answer on May 30, 2026 at 10:52 pm

    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");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The input string is something like this: LineA: 50 LineB: 120 LineA: 12 LineB:
my input String is : 2010-03-24T17:28:50.000Z output pattern is like: DateFormat formatter1 = new
i would like to take any input string and delimit it into groups. each
This is driving me nuts... I have an input string like so: String input
I have an input of a random string of twelve characters like this: ABABABABABAB
I am using below code snippet to validate my input string with: only capital
I'm having trouble separating numbers and characters from my input string. The purpose of
we are accepting all sorts of national characters in UTF-8 string on the input,
My input string is '16-MAR-2010 03:37:04' and i want to store it as datetime.
I have an input string which is a directory address: Example: ProgramFiles/Micro/Telephone And I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.