Thread: help scanning data

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    help scanning data

    I need to ask the user to input data and they want it all on one line like this:

    Enter data: 1 2 3 4 5 (user can put more than 5 numbers if they wish)

    I have to put this data into an array and I don't know how. I figure I'm going to have to make a file and put it into there and then read it from the file.

    So my question: How can I get this data into a file?

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Quote Originally Posted by Matt33
    I need to ask the user to input data and they want it all on one line like this:

    Enter data: 1 2 3 4 5 (user can put more than 5 numbers if they wish)

    I have to put this data into an array and I don't know how. ?
    something like this ? ...
    Code:
    #define MAXLINESIZE 128
    #define MAXNUMSPERLINE 20
    
    int nums[MAXNUMSPERLINE] = {0};
    char linebuff[MAXLINESIZE];
    
    //get your input with gets() or cin >> or whatever ...
    
    char *p = linebuff;
    int numindex = 0;
    
    while(TRUE){
    	while(*p==' ')p++;//absorb spaces
    	nums[numindex++] = atoi(p);//put num in array
    	if(numindex == MAXNUMSPERLINE){//check for too many nums here
    	//do something ...
    	}
    	p = strchr(p,' ');//find next space
    	if(!p)break;//no more !
    	p++;
    }

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM