Thread: Few simple problems

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    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.

    2. Displaying the £ sign in C++. If I do,
    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 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

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    For #1, if the floor() value of the float matches the float, then cast to int and print.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    For #2
    Code:
    char ascii = 156;
    std::cout<< ascii;
    Where 156 = £. GOOGLE ascii character map for more.
    Asking a question you already know the answer to might teach you something you did not know...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Bad, bad, bad!
    Special characters are usually a problem when written to the console. The only reliable way to do it, I believe, is to use unicode, but that opens a whole new bag of worms.
    I would say: avoid it. It isn't that necessary. There is at least one thread on the issue, I think.
    But under no circumstances rely on the ASCII value of something. It isn't portable, and extended ASCII is even less portable (will appear different on each machine).

    Also: SourceForge.net: Void main - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple 2d "putpixel" api for windows?
    By spiky in forum C Programming
    Replies: 2
    Last Post: 10-27-2005, 02:44 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 04:59 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM