0
   string name;
   float hours, wage, netwage, SocsoContribution, HoursBalance;
   cout<<"WELCOME TO XYZ";
   cout<<"\n";
   cout<<"Please enter your name: ";
   cin>>name;
   cout<<"Please enter your hours worked: ";
   cin>>hours;
   cout<<"\n";
   cout<<"\n";

   if(hours>60)
   {
       HoursBalance=hours;
       wage += (40*5)+(20*8);
       HoursBalance = hours - 60;
       wage += HoursBalance*10;
   }
   else if(hours>40)
   {
       HoursBalance=hours;
       wage += 40*5;
       HoursBalance = hours - 40;
       wage += HoursBalance*8;
   }
   else
   {
       wage = hours*5;
   }

   SocsoContribution = 0.01*wage;
   netwage = wage - SocsoContribution;

   cout<<"PAYSLIP";
   cout<<"\nName: "<< name;
   cout<<"\nHours worked: "<< hours<< " Hour(s)";
   cout<<"\nWage: RM"<< wage;
   cout<<"\nSocso Contribution: RM"<< SocsoContribution;
   cout<<"\nNet Wage: RM"<< netwage;

So above is a C++ code I wrote for a wage calculating program. But whenever I run the code and I input the name with space lets say "Mr Adam", the code will skip the input for hours and then it just execute the rest of the code without having the value for hours. I'm using codeblocks IDE.

  • You've got a lot of uninitialized variables here. Time to turn on more warnings. – tadman Oct 11 '22 at 03:51
  • 1
    You might be experiencing an error due cin is just reading the first string and stops reading when the space char is read. A solution could be to implement the getLine function as mentioned here: https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces – QSoto Oct 11 '22 at 03:57

2 Answers2

0

If you want to get whole sentence which you type one line,

You just replace cin to getline

#include <iostream>
#include <string>

int main()
{
    std::string name;
    std::string name1, name2;

    std::cout << "Name (with getline) : ";
    std::getline(std::cin, name, '\n');     // Mr Adam
    std::cout << "Name : " << name << std::endl;   // Name : Mr Adam

    // in case of std::cin ..
    std::cout << "Name (with cin) : ";
    std::cin >> name1 >> name2; // Mr Adam
    std::cout << "Name1 : " << name1 << std::endl;  // Name1 : Mr
    std::cout << "Name2 : " << name2 << std::endl;  // Name2 : Adam
        // std::cin is separated by spaces
}
mystes
  • 59
  • 7
0

I just put the suggestion of mystes in your code try this and you are good to go.

 #include<iostream>
 #include<string>

 using namespace std;

 int main(){
 string name;
 float hours, wage = 0, netwage, SocsoContribution, HoursBalance;
 cout<<"WELCOME TO XYZ";
 cout<<"\n";
 cout<<"Please enter your name: ";
 getline(cin, name);
 cout<<"Please enter your hours worked: ";
 cin>>hours;
 cout<<"\n";
 cout<<"\n";

   if(hours>60)
   {
     HoursBalance=hours;
     wage += (40*5)+(20*8);
     HoursBalance = hours - 60;
     wage += HoursBalance*10;
    }
    else if(hours>40)
    {
      HoursBalance=hours;
      wage += 40*5;
      HoursBalance = hours - 40;
      wage += HoursBalance*8;
     }
     else
     {
      wage = hours*5;
     }

     SocsoContribution = 0.01*wage;
     netwage = wage - SocsoContribution;

     cout<<"PAYSLIP";
     cout<<"\nName: "<< name;
     cout<<"\nHours worked: "<< hours<< " Hour(s)";
     cout<<"\nWage: RM"<< wage;
     cout<<"\nSocso Contribution: RM"<< SocsoContribution;
     cout<<"\nNet Wage: RM"<< netwage;

}