here's my code:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    char name[30], choice;
    int hours_worked;
    float wage;
    do
    {
        cout << "Employee Name: ";
        cin.getline(name,30);
        cout << "No of hours worked: ";
        cin>>hours_worked;
        cout << endl;
        wage = 80.75*hours_worked;
        cout << "The salary of " << name << " is " << std::fixed <<
        std::setprecision(2) << wage << " pesos"<< endl << endl;
        cout << "Do you want to quit? [Y/N] ";
        cin >> choice;
    }while (choice != 'Y' && choice !='y');
    return 0;
}

when i run it the display is:
Employee Name: John Green
No of hours worked: 100

The salary of John Green is 8075.00 pesos

Do you want to quit? [Y/N]

but when i answer Y the 1st and 2nd line merges and becomes:

Employee Name: No of hours worked:

and i dont kno why. thanks in advance!

Recommended Answers

All 2 Replies

I'd strongly recommend walking through your code with a debugger. And my big hint for the problem is that the Enter key places a character in the input stream: '\n'. When you mix formatted and unformatted input (eg. getline and the >> operator), there's a strong chance that newlines are being mishandled.

Please try this code
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    char name[30], choice;
    int hours_worked;
    float wage;
    do
    {
        cout << "Employee Name: ";
        cin>>name;
        cout << "No of hours worked: ";
        cin>>hours_worked;
        cout << endl;
        wage = 80.75*hours_worked;
        cout << "The salary of " << name << " is " << std::fixed <<
        std::setprecision(2) << wage << " pesos"<< endl << endl;
        cout << "Do you want to quit? [Y/N] ";
        cin >> choice;
    }while (choice != 'Y' && choice !='y');
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.