• Navigation
Results 1 to 5 of 5
  1. #1
    Sea Torques
    Join Date
    Nov 2005
    Posts
    504
    BG Level
    5

    Really basic computer programming question

    Hey everyone,

    I'm taking an introductory computer programming class this semester just to get a basic idea of programming (I'm thinking of maybe declaring a minor, but I wanted to see what it was like first). We have a project where we are supposed to read a file that has a series of integers that represent the number of seconds on CD tracks, then output in a table how much time is on each track (in minutes and seconds), and a running total of how much time is on the CD as each integer is read (also in minutes and seconds), as well as how much time is left on the CD (assuming it holds 80 minutes of music).

    What I'm wondering is how I could write a program that will continuously declare integers to store input from the file until it reaches the end of the file. I assumed I would do the second part with a while(!inFile.eof()) loop, but how can I set it up so it keeps storing input (not knowing how many tracks there are total)? Would I declare an array, or is there a simpler way of doing this? I've been looking around, but I'm not sure what to search for (we haven't really discussed anything like this in class).

    Thanks all, and if I'm not being clear, I'll try to explain it better.

  2. #2
    Relic Weapons
    Join Date
    Oct 2006
    Posts
    335
    BG Level
    4

    Apologizes in advance if this sounds condescending, because that certainly isn't the intent.

    First: reread your ENTIRE assignment. One tendency people have when it comes to programming homework is to over-complicate the task that's being given to them.

    Now, assuming what you've written in the first paragraph is accurately paraphrased, think about the problem. Do you NEED to maintain a list of the individual values you will be reading from the file? This is the reason why you should read the entire assignment, because a subsequent part of it may or may not ask you to perform tasks that require you to keep a list.

    Now, sit down and look at the individual tasks you need to complete. You need to:

    - open/close a file
    - read the contents of a file (by line, by token, whatever)
    - convert the file contents to a value(s) of time for each track
    - keep track of how much time is on the CD so far
    - keep track of how much time is remaining on the CD so far
    - present your data in a table form to the program user

    Is there a specific order that these subtasks have to be performed in? Do you have to finish one or more subtasks completely before you can complete the rest, or can you interleave them? Are there any subtasks that require you to have special data structures or storage concerns (e.g., a list of values representing the data you're reading in)?

    If you sit and think about how you want to program's basic blocks work together, it's pretty easy to answer the question of "what should I use to store input?"

    Spoiler: show

    Your assignment, as presented, does not need you to record your values in a list. Why? Your output only requires:

    1. the time on each track
    2. a running total of the time so far on the CD
    3. a running total of the time remaining so far on the CD

    #1 is calculated directly from the input you're reading in from each file, and #2 and #3 are just values for which you only need two actual accumulators (variables)--timeElapsed and timeRemaining--that you maintain as you read in each new track value.

    You can build your table (or rather, format and write [to] output) as you read your values from the file.

    For the rest:

    - When reading data from files, look at using ifstream.good()
    - Arrays are bad (but sometimes necessary and you should understand when you should and should not use them), explore vectors and other standard containers instead

  3. #3
    Sea Torques
    Join Date
    Nov 2005
    Posts
    504
    BG Level
    5

    I actually figured out that I was making it way more complicated than necessary, and just finished the assignment. In a previous assignment, we did what you suggested, breaking up the program into smaller subprograms to make it easier to organize what we needed to do--my design was somewhat flawed, but I was able to correct it. Thanks much!

  4. #4
    Relic Shield
    Join Date
    Jul 2008
    Posts
    1,951
    BG Level
    6
    FFXIV Character
    Audrey Weaver
    FFXIV Server
    Behemoth
    FFXI Server
    Asura

    This seems resolved, but next time you have a programming question, you might want to mention what language it's in and show what code you have so far. A lot of bad assumptions are very obvious to other people looking at your code - you just don't notice it cause you've been looking at it for too long.

  5. #5
    Sea Torques
    Join Date
    Nov 2005
    Posts
    504
    BG Level
    5

    You're right. My apologies--it was C++.

Similar Threads

  1. Replies: 16
    Last Post: 2012-05-08, 21:19
  2. Computer migration questions
    By Epical in forum Tech
    Replies: 11
    Last Post: 2010-01-16, 23:33
  3. computer/game question
    By gandorf in forum Tech
    Replies: 2
    Last Post: 2008-10-12, 01:27