Thread: String value into a int vector

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    String value into a int vector

    Dear Experts,

    please help me,

    what i am trying is i am reading a file and strtok on newline and sending those lines to a string vector then the contents of the file contain numbers like 12356 ....

    so i want to send these character by character from string vector to a int vector

    so at last i want al those values in the int vector how to achieve this can you please help me

    thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I guess that by vector you mean dynamic array? From what you describe, it may be easier to use getchar() instead of strtok().
    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

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Laserlight's right. Here, watch this execute; while far from ideal, it should give you some ideas.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void) 
    {
    	FILE *infile = stdin; /* read from keyboard */
    	
    	int vector[255];
    	int elem;
    	size_t count;
    	const size_t N = sizeof vector / sizeof vector[0];
    	
    	for ( count = 0; count < N; ) {
    		elem = fgetc(infile);
    		if ( elem != EOF ) {
    			if ( isdigit(elem) ) {
    				vector[count] = (int)( elem - '0' );
    				printf("vector[%d] = %d\n" , count , vector[count]);
    				count++;
    			}
    		}
    		else {
    			break;
    		}
    	}
    	
    	return 0;
    }
    Last edited by whiteflags; 11-21-2009 at 05:00 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oops, yes, getchar() reads from stdin, whereas this is supposed to read from file.
    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. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM