Thread: how to read numbers in a better way?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    how to read numbers in a better way?

    In case of i don't know how many double numbers the users will input, can any one suggest a good way for me to read the input numbers?
    i need to write a program to store a Matrix with dimentions input by the users and do some operation, so i want to have a better way to store the matrix.
    but i can't use the command line option argument as a matrix have both columns and rows.
    thx a lot

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Vectors, google them.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i can't use the command line option argument as a matrix have both columns and rows
    I don't see what one thing has to do with the other, why do you think columns and rows stops you from using command line arguments?

    You can just ask the user to input the dimensions first, then read in values until you fill up the matrix.

    Whether you get the dimensions via the command line or from asking the user, you'll probably want to dynamically size the matrix. If you use vectors, it is easy to set the size at runtime when the user inputs it. Whether you use a vector of vectors, or a single vector that simulates two dimensions, you can size it after you get the input.

    If you can't let the user give the dimensions beforehand, you can still read in a line as a string using getline, then put the string into a stringstream to get each value on the line. You can push those values onto the vector with push_back.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf function won't read my float value...?
    By qpsnhalfs in forum C Programming
    Replies: 8
    Last Post: 07-07-2009, 11:01 PM
  2. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  3. Read large amount of numbers from file
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 05-10-2007, 09:33 AM
  4. Read formatted numbers from a txt file
    By xldie in forum C Programming
    Replies: 1
    Last Post: 12-19-2006, 10:43 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM