Thread: Struggling to understand C, and where I am going wrong.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    Struggling to understand C, and where I am going wrong.

    Hi all

    I am a mature student, working and doing night classes to complete my Degree. I have read the link on homework and I understand the policy, not looking for answers just need some help to understand

    Here is my current problem I have to load CSV data from a file into what I think must be an array or struct of arrays I then need a main program that prompts the user for options. These will be modular functions in a menu list that would ADD DELETE EDIT PRINT the data stored in the array and Save the list on exit of the main program

    So what I can do is open the file with fopen error check it with perror and use fgets to capture the lines of CSV data to a Char variable. I can also use puts to print the CSV data to the screen.

    The problem I have is to wright the ADD function in a way that the Load function can call it to save the CSV lines to the array.

    Any advice would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Welcome, and thanks for reading up on the forum rules! You sound like you're mostly on the right track with your file handling.

    First, I think you want an array of struct, not a struct of arrays. Think about the CSV file. Each line typically contains one record, with a mix of data, like name, address, telephone number or something. For grouping mixed data, you use a struct. The CSV file has some number of these records. Since each line in the CSV file (and each struct) contains the same type of data, you want an array of those. An array is a collection of all the same type of things.

    You probably want printf to print the data to the screen (or fprintf for a file when it's time to save), since it allows formatting of the integers, strings, etc in your records.

    You didn't specify whether the records have to be in some order, so I'm assuming they don't. That makes the add function easy. You already have your array, and you (hopefully) have a count of how many records are actually stored in your array (perhaps it's called record_count). Thus, if you have 7 records (array indexes 0 through 6), then record_count is 7 and your first open spot is array[7], or array[record_count], so that's where you add the record. Then you increment the record_count variable, and you're all done.

    Some general advice: don't jump right into coding. Programming is not about typing, it's about problem solving. You can't write a program if you don't understand the problem. So take time to understand the problem. Work out a solution on paper, (more or less) in English. Then turn that solution into pseudo code and finally turn that pseudo code into real C code. Only write 5-10 lines of code at a time, compile (with maximum warning level) and resolve all warnings, and make sure your new code does what it's supposed to, before moving to the next 5-10 lines. This will make your work go much more smoothly, result in less bugs, and make it easier to find and fix the few bugs you do have.

    When it comes to coding a project like this, I would start with the basic menu, and make sure that you can read menu choices, select the right action (just print something like "Record added"), and handle invalid menu choices. Then, I might work on print, so you can easily see if your data is correct when you get around to implementing add, delete, edit, save, load, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Don't understand what's wrong
    By AndreyS10 in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2011, 11:41 AM
  2. Replies: 39
    Last Post: 01-18-2009, 11:53 PM
  3. I don't understand what is wrong...
    By flaran in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2006, 06:46 AM
  4. Can't understand what I'm doing wrong.
    By agentxx04 in forum C Programming
    Replies: 11
    Last Post: 10-22-2004, 07:53 PM
  5. I don't understand what i'm doing wrong..
    By knutso in forum C Programming
    Replies: 4
    Last Post: 03-15-2002, 07:37 AM