So I just started C++ and have been reading through notes and working on tasks given to me by my lecturer. I have came across a few things I can't figure out.
1. Removing decimal places on a float number if it comes out as whole number.
Code:
sum = firstNumber + secondNumber + thirdNumber;
average = static_cast<float>(sum)/3;
cout.precision(2);
cout << "The average is: " << fixed << average << endl;
Say average turns out to be 6, it will display as 6.00. I want to remove the decimal points so it displays as 6. But if it comes out like 6.53, I want to keep it like that.
2. Displaying the £ sign in C++. If I do,
It will display something like "ủ".
3. Using strings in loops.
Code:
#include <iostream>
#include <string>
using namespace std;
void main()
{
float hours, salary, rateOfPay;
char reply;
string name;
do
{
cout << "Please enter the name: ";
getline(cin,name);
cout << "Please enter the rate of pay: ";
cin >> rateOfPay;
cout << "Please enter the amount of hours worked: ";
cin >> hours;
salary = rateOfPay * hours;
cout.precision(2);
cout << "The salary is: " << fixed << salary << endl;
cout << "Do you have any more employees? ";
cin >> reply;
} while (reply !='N');
}
When this code is executed it works fine the first time, when I press Y for more employees, it completely skips getline(cin,name);.
My code when ran looks something like this.
Please enter the name: Jim Murray
Please enter the rate of pay: 10
Please enter the amount of hours worked: 10
The salary is: 100.00
Do you have any more employess? Y
Please enter the name: Please enter the rate of pay: 10
Please enter the amount of hours worked: 10
The salary is: 100
Do you have anymore employees? N