>>But know how I can access to my values in array
Is that the question -- how to extract each of the numbers in the string ? extract them using sscanf(). There is a simple example
int main()
{
char line[] = "123 234.56 789";
int a, b;
float c;
sscanf(line,"%d %f %d", &a, &c, &b);
printf("%d %f %d", a, c, b);
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
question:
does each line of your data always have the same number of INTs and FLOATs, and are they always found in the same order?
if so, Dragon's suggestion for "sscanf()" is perfect. if not, you will need a more complex routine, perhaps using "strtok()"
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
>>strcpy(input[i],line);
What's the initial value of i? Answer: undefined because you didn't set it to 0 before that loop started.
>> while (line[i]!=EOF)
line[i] will NEVER EVER be EOF. So that's the wrong way to code that loop. What you need to do is create another integer that counts the number of lines read so that in this loop you can check the number of lines. You have already done that to an extent -- in the loop that reads the data you are using variable i but then the program uses that same variable over again. Use a different counter.
for(i = 0; i < counter; i++)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
In order to learn how to program you first need to learn how to read. Please read my previous very very carefully and you will find the answer to your problem.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
HI Sir
I wish to write a program that can do the following
I can assure you that no one over here will prevent you from doing so.
It is customary to read the rules of the forum if you are posting for the first time.
Allow me to invite you to do that now. Click here
For further help read this and this
And one more thing. It is alright to make your own new thread, next time you want to ask a question.
Welcome!! :)
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
I can assure you that no one over here will prevent you from doing so.
i might.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
i might.
Oh, yeah? I would like you to try, stupendous man ;)
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
silly mortal, you doubt my powers?
watch me battle my way through pedantic digressions about the dangers of pseudo-randomness and the evils of scanf, while flying by the seat of my pants deep into the lair of non-error-checked command line input. all in the name of clarity i say!
whoops... here comes Naure
*ahem* now if you'll excuse me, i believe i have other um... threads to rescue.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
for(i=0,cnt=0;i<cnt;i++,cnt++)
Well, if i = 0 and cnt = 0 this i < cnt will never allow the loop to execute, because i is never smaller than cnt. Don't you think?
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
I know the loop don't enter. if you see i mentioned that i my question.
Yes, he saw that. He also explained your problem for you, so that you can come up with a solution yourself.
This is a great attitude if you want people to ignore your problem, but I'm in a fairly good mood, so: for(int i=0; i< (how many elements are in your array); i++)
Now remove all the cnt's because they don't make any sense.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Yes, he saw that. He also explained your problem for you, so that you can come up with a solution yourself.
.
he :icon_eek: Aia's avatar looks female to me :)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
he :icon_eek: Aia's avatar looks female to me :)
Whoops. I have avatars switched of, so I didn't notice it... :icon_redface:
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
he :icon_eek: Aia's avatar looks female to me :)
Whoops. I have avatars switched of, so I didn't notice it... :icon_redface:
Vanity, oh, vanity.
Everything is vanity.
What today is,
Tomorrow does not exist.
Must appearance still persist?
Forgive me for the indulgence of putting the thought into rhyme.
I'm not quite sure of what you want. Perhaps something like this?
for ( i = 0; i < 150; i++ )
{
if ( sscanf( input[i], "%f", &num[i] ) == 1 )
printf( "My Numeric Array[%d] = %f\n", i, num[i] );
else
break;
}
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218