How do i use the split() to seperate numbers

This is a discussion on How do i use the split() to seperate numbers within the C++ Programming forums, part of the General Programming Boards category; #include <iostream.h> #include <conio.h> int main() { double Number, Decimal; int Integer; cout << "Enter decimal number: "; cin >> ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    How do i use the split() to seperate numbers

    #include <iostream.h>
    #include <conio.h>
    int main()
    {
    double Number, Decimal;
    int Integer;
    cout << "Enter decimal number: ";
    cin >> Number; cin.ignore();


    cout << "\nIntegerl: " << Integer << " and decimal: " << Decimal;
    cout << "\n\npress a button"; getch();
    return 0;
    }

    So if i enter 19.84444

    I want it to say Integer 19 and decimal 0.84444

    I have no idea how to use the split() function
    Can someone please help ?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Which "split" function are we talking of here? As far as I'm aware, there is no standard function called "split".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    split isn't a standard function. Search your C++ reference for modf, that does what you want.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Quote Originally Posted by Prelude View Post
    split isn't a standard function. Search your C++ reference for modf, that does what you want.
    Cheers

    I found modf

    double modf(double x, double *y);

    That's all that's written about it, could you please show me how to use it ?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    First hit on google, and it has an example: http://www.cplusplus.com/reference/c...math/modf.html
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Fantastic, i just googled the split() and came up with nothing

    Cheers

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Another option (just for the sake of expressing alternate methods)

    Example:
    Code:
    #include <sstream>
    #include <iostream>
    
    int main(void)
    {
      std::stringstream s;
      float x;
    
      std::cout << "Please input a number: ";
      std::cin >> x;
    
      s << x;
    
      std::string whole, decimal;
      size_t pos = s.str().find(".");
    
      whole = s.str().substr(0,pos);
      decimal = std::string("0") + s.str().substr(pos);
    
      std::cout << whole << " and " << decimal << std::endl;
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to reverse numbers with sizeof operator
    By JoelearningC in forum C Programming
    Replies: 13
    Last Post: 03-09-2008, 12:53 PM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 03:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21