Thread: Storage Types, formatted input/output, processing a menu help.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    8

    Storage Types, formatted input/output, processing a menu help.

    Hi, So I have a program for school that is attached below, and my main problem is knowing where to start and how it should be structured. Can someone give me an idea on how I should go about writing this program, like should i make functions, pointers, etc. And to display the menu, is it easiest to just use printf statements or is there something more efficient. Any help on how to get started on this would awesome. Thanks
    Attached Images Attached Images

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The specifications are quite clear. What have you tried so far?

    Any help on how to get started on this would awesome.
    The first steps should not involve code at all. Determine what you have to find out, and how you need to go about finding it. Figure out the steps you need to follow in order to satisfy the requirements of the program. Do it with paper and a pen, if need be. Planning the program you are going to write will help you write the code more easily and efficiently.

    You can start with a simple program - a "main()" function that (for example) prints "test" to the screen. Compile and run, and make sure the project is in good working order.

    Then pick somewhere to start, based on the preparation you did beforehand. For instance, you can start with "read a first name." Create the variables you need (just for reading in a first name) and write the code to accomplish this one task. Compile, run, and test. Fix any errors and bugs.

    Look at the next small step you want to accomplish, and incorporate that little bit into the program. Compile, run test.

    Repeat this process.

    And read this: A Development Process

    --------

    When you code like this, most of the questions you asked will answer themselves.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    To (mostly) answer your questions above: yes, you will need functions and pointers, and printf is fine for menus. I suggest fgets for reading data from the user, it reads a whole line (everything they type, until, and including when they press enter).

    A general approach I use:

    1. Read the problem, and make sure you understand what it is asking
    2. Figure out how to solve this by hand, on paper. Ignore things like details of opening a file, whether to use while or for loops, etc. Come up with a solution in plain English, or whatever your native language is. Note, if you don't know how to solve this yourself, there is no way to program a computer to do it.
    3. Try out your solution. Run through some examples with paper and pencil. You can make a fake "file" with data, on a separate sheet of paper, and "read" through that one char at a time. Keep track of every little step you take, write it down. That will be the basis for your algorithm.
    4. Take those steps written down in English, and translate them to pseudo code. Psuedo code should have roughly the same basic structure as your actual code, but you get to ignore the details and some of the error checking for now.
    5. Once you have your pseudo code, begin turning it into real C code. Work in small chunks, write no more than 5-10 lines at a time, then test that. By test, I mean compile your code with maximum warnings, and fix all errors and warnings, and run your code to make sure it behaves the way you expect.
    6. If you encounter problems, fix them immediately, before you move on. That way, if an error pops up, you know it was in the last 5-10 lines of code, and it's easy to fix.
    7. Continue the previous two step until you have fully implemented your solution to the problem.



    Good news for you, you have been given a decent program specification. You are told the exact menu options. When it's time to write code (which is not yet), I would probably start with the menu. Make sure your menu works, so it will be easy to test the other functions as you add them.

    Once your menu is working, I would work on defining the data structure you will use. You are told the exact type of data for each piece of info you want on the person, along with limits (e.g. age is 18-99 years, 32 chars for names) for validating input. If you have learned about structs, I would use a struct to hold all this info.

    Some more tips:

    • I would probably have my main function only declare the necessary variables, and call the main menu code. Let the functions do their jobs.
    • Make sure you use good, clear names. A function like clearAllData would be an example. It would clear all the values, setting them to whatever "unknown" values you use.
    • Use plenty of functions. There should be at least one of these per menu item, possibly more.
    • Use sensible values for "unknown" info. For example, an age can't be -1, so you could use that to represent "unknown age". Then, when you print (in your printData function), if age was -1, you would print "Age: unknown", otherwise, print "Age: 42" or whatever. A string like firstName could be length 0 (firstName = '\0') to signify unknown.



    That should be plenty to get you going.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    Quote Originally Posted by anduril462 View Post
    To (mostly) answer your questions above: yes, you will need functions and pointers, and printf is fine for menus. I suggest fgets for reading data from the user, it reads a whole line (everything they type, until, and including when they press enter).

    A general approach I use:

    1. Read the problem, and make sure you understand what it is asking
    2. Figure out how to solve this by hand, on paper. Ignore things like details of opening a file, whether to use while or for loops, etc. Come up with a solution in plain English, or whatever your native language is. Note, if you don't know how to solve this yourself, there is no way to program a computer to do it.
    3. Try out your solution. Run through some examples with paper and pencil. You can make a fake "file" with data, on a separate sheet of paper, and "read" through that one char at a time. Keep track of every little step you take, write it down. That will be the basis for your algorithm.
    4. Take those steps written down in English, and translate them to pseudo code. Psuedo code should have roughly the same basic structure as your actual code, but you get to ignore the details and some of the error checking for now.
    5. Once you have your pseudo code, begin turning it into real C code. Work in small chunks, write no more than 5-10 lines at a time, then test that. By test, I mean compile your code with maximum warnings, and fix all errors and warnings, and run your code to make sure it behaves the way you expect.
    6. If you encounter problems, fix them immediately, before you move on. That way, if an error pops up, you know it was in the last 5-10 lines of code, and it's easy to fix.
    7. Continue the previous two step until you have fully implemented your solution to the problem.



    Good news for you, you have been given a decent program specification. You are told the exact menu options. When it's time to write code (which is not yet), I would probably start with the menu. Make sure your menu works, so it will be easy to test the other functions as you add them.

    Once your menu is working, I would work on defining the data structure you will use. You are told the exact type of data for each piece of info you want on the person, along with limits (e.g. age is 18-99 years, 32 chars for names) for validating input. If you have learned about structs, I would use a struct to hold all this info.

    Some more tips:

    • I would probably have my main function only declare the necessary variables, and call the main menu code. Let the functions do their jobs.
    • Make sure you use good, clear names. A function like clearAllData would be an example. It would clear all the values, setting them to whatever "unknown" values you use.
    • Use plenty of functions. There should be at least one of these per menu item, possibly more.
    • Use sensible values for "unknown" info. For example, an age can't be -1, so you could use that to represent "unknown age". Then, when you print (in your printData function), if age was -1, you would print "Age: unknown", otherwise, print "Age: 42" or whatever. A string like firstName could be length 0 (firstName = '\0') to signify unknown.



    That should be plenty to get you going.


    Thank you very much, this was very helpful along with the other person. It definitely gave me a starting point to work on and I will post more if I need any help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatted output %*s
    By dtow1 in forum C Programming
    Replies: 3
    Last Post: 12-15-2012, 12:29 PM
  2. A little question about formatted output
    By Extropian in forum C Programming
    Replies: 10
    Last Post: 08-19-2005, 08:30 AM
  3. Number input and formatted output
    By scoobasean in forum C++ Programming
    Replies: 4
    Last Post: 12-20-2004, 11:17 PM
  4. formatted output
    By Magica in forum C Programming
    Replies: 3
    Last Post: 05-11-2003, 11:36 PM
  5. formatted file output
    By ronin in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2002, 10:38 AM