Thread: Accepting Decimals

  1. #1
    Registered User dragon_man's Avatar
    Join Date
    May 2003
    Posts
    6

    Question Accepting Decimals

    Is there a library or header file that gives a command that accepts decimals? I have been trying to get decimal input from the user but when I output it, it only prints the numbers before the decimal. Either a header file or I am doing something wrong.

    Here is what I am trying to make (example):
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
      long a;
      system("CLS");
      cout<<"Please enter a decimal number: ");
      cin>>a;
      cout<<"\nThe number you entered is "<<a<<endl;
      system("PAUSE");
      return 0;
    }
    
    // Here is what I got:
    /*
    Please enter a decimal number: 1.23
    
    The number you entered is 1
    Press any key to continue...
    */

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You have to use float or double for decimals. Int and long can't handle them.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Don't use header files with .h extensions. For the equivalent header files and an explanation of the using directive you also need with the new header files see here:

    http://www.cplusplus.com/doc/ansi/hfiles.html


    Get rid of:
    system("CLS");

    Change this:
    system("PAUSE");

    to:
    cin.get(); //waits for the user to hit any key

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Originally posted by 7stud

    Change this:
    system("PAUSE");
    to:
    cin.get(); //waits for the user to hit any key
    Any key that has enter written on it
    I find it's a lot easyer to just run console apps from, well, the console.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 04:13 PM
  2. Trouble with Decimals? =(
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 08:58 PM
  3. allow decimals
    By dankassdann in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 06:41 PM
  4. Getting enough decimals
    By Drogin in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2005, 09:37 PM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM