Thread: Reading file into stack

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Reading file into stack

    This is my input file:
    02/15/1995 S 10000 3.71
    02/15/1995 B 10000 3.71
    02/15/2009 B 1000 16.15
    01/27/2011 S 5000 28.87
    09/02/2011 B 100 25.80
    09/06/2011 B 500 25.51
    09/08/2011 S 700 26.52
    09/09/2011 B 2000 25.74
    09/15/2011 S 1000 27.00
    09/22/2011 B 300 25.00
    09/22/2011 s 30000 25.00
    09/26/2011 B 100 25.50

    How can I read this into a stack and then be able to process it? Should I use a typedef struct, a linked list, or what? I want to be able to access each field separately... How would I go about this?

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Without more information, I suggest the use of a struct and an array.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Use a struct (which you can typedef if you want to, but it's not necessary) to hold your data fields. You might use another struct within that struct to hold the separate date fields.
    Code:
    struct Date {
        int month;
        int day;
        int year;
    };
    
    struct PickAName {
        struct Date  date;
        char         whatever;
        int          something_else;
        double       blah_blah;
    };
    You can implement a stack with a linked list if you want.

    But why do you want to read the data into a stack?
    Last edited by oogabooga; 10-09-2012 at 03:19 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by oogabooga View Post
    But why do you want to read the date into a stack?
    It appears to be a requirement for the assignment.
    Algorithm Help Please!

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    I need to read through the file line by line and whenever I encounter an 'S' it means that a stock is being sold so I process the 'B'(buys) above it and calculate how many were sold multiplied by the amount that they were sold for.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Yep! That's the one. I figured out how to get the input file to work and the header file. But now I need further help figuring out the algorithm.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    So you were given a header file for a stack but you have to write the implementation yourself?

    Can you post the header file?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by oogabooga View Post
    So you were given a header file for a stack but you have to write the implementation yourself?

    Can you post the header file?

    The header file just has all of the basic stack operations such as: create stack, push/pop stack, delete stack, stack count, etc.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You really should have stuck to one thread. You should have learned at least one input function in class. Make an attempt at reading your file please. I actually think it is against the rules to do hand-holding the whole way.
    The purpose of these board is not for other people to do your homework for you. Try things out work on your own, homework has a purpose. If you still have trouble with a specific piece of code or concept please feel free to ask. But please do not ask people to do your entire homework for you, it simply annoys people most of the time.
    I'm starting to think you're doing that, tbh.

    It's belated, but welcome to the forum.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by hotshotennis View Post
    The header file just has all of the basic stack operations such as: create stack, push/pop stack, delete stack, stack count, etc.
    Post it. I want to see the types of the parameters.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by whiteflags View Post
    I actually think it is against the rules to do hand-holding the whole way.
    That absolutely should be a rule.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  2. Declaring size of stack from input file
    By alpine in forum C Programming
    Replies: 3
    Last Post: 03-22-2011, 06:43 PM
  3. Cannot open include file: 'stack.h'
    By GSalah in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2007, 03:18 PM
  4. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM
  5. stack make file problem
    By puckett_m in forum C Programming
    Replies: 2
    Last Post: 11-22-2001, 11:51 AM