Thread: using cin to get a float

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    28

    using cin to get a float

    I can't get cin to take a type float from the keyboard. I am entering them into an array with a for loop but when I put an 'f' at the end to indicate a type float I get kicked out of the loop. If I just initialize the array with '10.2f' for example it works fine.???

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cctype>
    using namespace std;

    const int ELEMENTS = 10;
    void arySorter(float *p, size_t n);

    int main()
    {
    int i;
    float ary2Sort[ELEMENTS]/* = {12.2f,1.1f,9.9f,2.2f,6.6f,44.4f,7.7f,3.3f,5.5f,4. 4f}*/;
    float *sortPtr;
    sortPtr = ary2Sort; // init sortPtr

    for(i = 0; i < ELEMENTS; ++i) { //get numbers
    cout << "Enter a number to be sorted: " << endl;
    cin >> ary2Sort[i];
    }

    cout << "\nPrint unsorted array" << endl;

    for(i = 0; i < ELEMENTS; i++) // print the array
    cout << ary2Sort[i] << endl;
    rc7j

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You mean when the program is running and you fill the array it chokes on something like 1.4f? That is to be expected, cin is expecting a float and you give it a float then a character. When the program is running all you have to do to enter a float is type the number, such as 1.4, and it should work fine.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    28

    Thanks

    Thank you. The program does run fine.
    rc7j

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    You sure it's not promoting the floats to doubles?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    28

    It does not promote to double, Thanks

    I used the following code to check it. THey are floats in the end.

    cout << sizeof(float) << "double" << sizeof(double) << " element " <<
    sizeof(ary2Sort[0]) << endl;

    THe output was "4double8 element 4"

    Thanks for the idea.
    rc7j

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You sure it's not promoting the floats to doubles?
    cin works in such a way so that if you tell it to read in a float it will read a float and not promote it to a double. If the variable you are reading into is a double then the end result will be a double, cin is a smart input device and only reads the type of the variable you tell it to read to.

    This is why you can use
    int i;
    if ( cin>>i )
    to determine if the input was valid or not, if the user entered a char then cin will fail and the if statement will fail as well.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    28

    good idea

    That error checking is great. I will put it in my program. Thanks
    Last edited by rc7j; 02-24-2002 at 01:39 PM.
    rc7j

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM