Thread: printing to an array

  1. #1
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19

    [**RESOlVED**]printing to an array

    i have a text file that contains numbers and i am reading them in line by line, this is not a problem

    but the problems come when i try to get these numbers and put each number in the array, for some reason it just seems to take the first character (either a 0 or a -) and it printts out it's ASCII equivialant and i don't want this

    any ideas on how to remedy this ??

    here is the code so far

    any help will be appreciated

    cheers
    Last edited by zmerlinz; 04-29-2003 at 07:59 AM.
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's because you are reading a character, and you're assigning it as a character:
    Code:
    numbers[i] = (double)*nums;
    This says "Give me the decimal value of whatever character I read, and use that as a float.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19
    the file that i am reading in contains a long double on every line

    so how would i remedy it so that it enters these into the array ?

    thankyou for your speedy response
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Assuming your compiler / libraries provide something like atof, you can just use that. Otherwise, you'll need to use something like sscanf to read it into a variable.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19
    so if i were to use sscanf()

    i would do something like this ?/

    Code:
    sscanf(nums, numbers[i], float);
    sorry for all the questions but this is beginning to bug me
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  6. #6
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19
    atof doesn't seem to work properly

    here is a copy of the txt file if it helps ??
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by zmerlinz
    so if i were to use sscanf()

    i would do something like this ?/

    Code:
    sscanf(nums, numbers[i], float);
    sorry for all the questions but this is beginning to bug me
    No, it'd be something like this:

    sscanf( nums, "%f", &numbers[i] );

    sscanf works just like scanf, except you specify where you scan from as the first argument.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or better still, use strtod(). The FAQ shows some examples of getting numbers.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19
    Originally posted by quzah
    No, it'd be something like this:

    sscanf( nums, "%f", &numbers[i] );

    sscanf works just like scanf, except you specify where you scan from as the first argument.

    Quzah.
    i have done that now, but it still doesn't seem to work, all it prints out is 1000 lines of 0.000000000000000

    so it seems that it isn't working with those values at all

    any idea where it is going wrong ??

    Cheers
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  10. #10
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19
    ok

    here is the C file and text file that it is reading in
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  11. #11
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19
    and here is the txt file

    thanks for your help
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  12. #12
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19
    i see what you say, and i have implemented that now,

    but it still prints out 1000 lines of 0.000000000000000

    it is driving me mad !!
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

  13. #13
    Registered User zmerlinz's Avatar
    Join Date
    Apr 2003
    Posts
    19

    Talking

    Thank you ever so much,it is finally working

    and i have learnt a lot from this to

    sorry about all the questions


    Thanks to all that helped
    Why is it all Mafia bosses are called Don? I mean, Donald isn't even an Italian name
    [Galahtech.com] | [My Site] | [Fishsponge] | [UnixForum.co.uk]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing half a char array
    By theoneandonly in forum C Programming
    Replies: 19
    Last Post: 11-11-2006, 07:27 AM
  2. Replies: 3
    Last Post: 11-03-2003, 08:55 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM