Home > Net >  My code skips the next input when the first input entered
My code skips the next input when the first input entered

Time:10-11

   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.

CodePudding user response:

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
}

CodePudding user response:

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;

}
  •  Tags:  
  • c
  • Related