Thread: Converting numbers to characters?

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

    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;
    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;
    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
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    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
    while( 4==fscanf(filepointer,"%d%lf%lu%d",&aint,&adouble,&aulong,&anotherint) )
      if( anotherint )

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, 09: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, 06:38 PM