Converting numbers to characters?

This is a discussion on Converting numbers to characters? within the C Programming forums, part of the General Programming Boards category; Hey all! Finals are over, yes! Ended up with a C in my programming class, and that is really ok ...

  1. #1
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18

    Converting numbers to characters?

    Hey all! Finals are over, yes! Ended up with a C in my programming class, and that is really ok with me. Like another user on here said, it is like learning another language, and I was really bad a Spanish :-) But I had a program that was assigned for homework that I never was able to figure out. Since I am the curious sort, I was wondering if anyone wanted to take a crack at it?
    The instructions are:
    For this program, you are reading from a file, data5.dat. The first column are sequential numbers starting at 1, corresponding to the line number. The second column gives a piece of data (floating point). The third column gives the date and time the data was taken (should read the 10-digit number as an integer long). The forth column contains 1's or 0's that denote if the data is good or not. 1 is good, 0 is bad, therefore discarded.

    The data5.dat file I wrote looks like this;
    Code:
    1  5.421       1202960413  0
    2  -223.432  1202960413  1
    3  -2.14         226991307   1
    4  22.222      905842300   1
    5  -27.1234   826430523   0
    6  1000.00    826430523   1
    7  -27.753   1010071201   1
    For the first part, I am supposed to print the good data to the screen. The second part adds the line numbers in, renumbered (so, only the good data, plus the line it is now on). The third part gives the date with the months spelled out. And the forth part adds the time onto the end of it. So, for line 1, the final output should look like;
    Code:
    1  -223.432  December 2, 1996 at 4:13 AM
    I was thinking for the first part, the part where you discard the bad data, that I would use a flag inside a for loop. But not sure how I would initialize it, since the numbers are in the 4th column.

    Anyway, anyone feel like taking a crack at it? More for my own knowledge than anything, since I will more than likely be using C again at some point in my engineering education :-)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by Aslan14
    I was thinking for the first part, the part where you discard the bad data, that I would use a flag inside a for loop. But not sure how I would initialize it, since the numbers are in the 4th column.
    Instead of immediately formatting and printing the line, read the contents of the entire line into say a struct. If the flag is 1, format and print the struct, then move on to the next line.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Since the input file is properly formatted, you could just use fscanf() to read the numbers.
    If the fourth column number is zero, don't output anything.

    I'd suggest to read the 3rd column data as string. Then it would be easier to do your third and fourth part.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    while( 4==fscanf(filepointer,"%d%lf%lu%d",&aint,&adouble,&aulong,&anotherint) )
      if( anotherint )
        puts("good");
      else
        puts("bad");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Bytes to Numbers
    By Bladactania in forum C Programming
    Replies: 9
    Last Post: 03-27-2009, 10:34 AM
  2. How would I only enter numbers and not characters
    By chrismax2 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2004, 03:19 PM
  3. Converting Numbers to Words
    By denizengt in forum C Programming
    Replies: 20
    Last Post: 11-05-2003, 08:19 PM
  4. converting numbers into words
    By Kozam in forum C Programming
    Replies: 2
    Last Post: 09-30-2003, 07:49 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21