# Few simple problems

• 12-21-2009
Crashie
Few simple problems
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.

Code:

`cout << "£";`
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 amount of hours worked: 10
The salary is: 100
Do you have anymore employees? N
• 12-22-2009
Dino
For #1, if the floor() value of the float matches the float, then cast to int and print.
• 12-22-2009
hk_mp5kpdw
For #3
Code:

```do {     cout << "Please enter the name: ";     getline(cin,name);     ...     cout << "Do you have any more employees? ";     cin >> reply; } while (reply !='N');```
The problem is with the cin>> in red above. After you've grabbed the Y/N reply and stored it, the newline character remains in the input stream/buffer. It is not extracted along with the Y/N character. When the loop returns to the getline, the newline is then extracted and the program continues thinking that you the user has pressed the enter key. You need to extract the newline character somewhere after the cin>> call and prior to the getline call. I'd suggest a call to cin.ignore() immediately after the cin>> call.
• 12-22-2009
QuestionKing
For #2
Code:

```char ascii = 156; std::cout<< ascii;```
Where 156 = £. GOOGLE ascii character map for more.
• 12-22-2009
Elysia