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++;
}