Vector Consists of Both Int and String, HELP

This is a discussion on Vector Consists of Both Int and String, HELP within the C++ Programming forums, part of the General Programming Boards category; Hello, Let's say i have a text input file consist of numbers and letters. How should i put them into ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    15

    Vector Consists of Both Int and String, HELP

    Hello,

    Let's say i have a text input file consist of numbers and letters. How should i put them into an vector. I want numbers to stay as int, letters as strings.

    Code:
    void PutInputToArray(ifstream &dfa,tvector<string> &v)
    {
    	string word;
    
    	  while(dfa >> word)
    	{
    		  v.push_back(word);	  
    		
    	}
    	  
    
    
    }
    In this code input file reads as it's input is string. What should i do? Should i use templates? How?

    Thanks.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What do the numbers and letters in the input file mean? I'm sure there's a better way to store them than a heterogeneous vector. (Which is possible, but messy and against the spirit of C++.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    I'd store them all as strings and convert them later. If you want to remember what type they are because you only know that at the time you read in, then consider adding more state to an object that holds the type and value, then store instances of that object in the vector.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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