0

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!

Edited by something_1

3
Contributors
2
Replies
34
Views
11 Months
Discussion Span
Last Post by Usman_10
0

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.

-1
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;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.