Getline dealing with integers

This is a discussion on Getline dealing with integers within the C++ Programming forums, part of the General Programming Boards category; Say you have a file formatted like this (name, phone number): Joe Smith, 9019809923 Greg Jones, 9019867823 and you use ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    Getline dealing with integers

    Say you have a file formatted like this (name, phone number):

    Joe Smith, 9019809923
    Greg Jones, 9019867823

    and you use getline to read in the each line then push it into a vector of strings.

    so, you use
    Code:
    getline(cin, name, ','); //ignore the comma
    get the line, then push it into the vector like this:
    Code:
    names.push_back(name);
    This pushes Joe Smith into vector(0) and the corresponding phone number into vector(1), and the same with the second name into vector 2 & 3.

    Is there any way to 'rip' out the phone numbers and properly format it like this: 901-980-9923?
    I do not need help formatting the number... my question is how to you rip out a string and convert it into an integer - to properly use % to get it to format it correctly?

    Any help greatly appreciated.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    char* strContainingNumbers = "1234";
    int num = 0;
    
    std::stringstream ss; //located in  <sstream>
    ss << strContainingNumbers;
    ss >> num; // num == 1234

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,816
    I do not need help formatting the number... my question is how to you rip out a string and convert it into an integer - to properly use % to get it to format it correctly?
    I suggest that you do not bother with converting it to an integer (though it can be done). Consider that these numbers, as integers, are likely to be greater than 2^32-1, which may be problematic.

    As a string, a simple series of substrings and concatenation would suffice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  4. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM

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