Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ program not working, need help

  1. Nov 7, 2006 #1
    I've been trying for days to get my boyfriend's C++ program to work, and I am almost ready to concede defeat, but I have frequented this site in the past as a lurker, actually learning a lot of what I know from the tutorials (thanks!).

    Now, I have a problem I just can't seem to solve.

    The object is to have the user pick a choose from a menu and have that value stored somewhere to be used by a function later. Once that choice is picked, the user needs to be able to pick another choice, until he or she fills in all three choices and then it's on to the formula! I also need a choice for clearing the data (I know how to set it all to 0) and one for quitting the program (just return 0; right?), as well as a display of the data that has been entered already once the formula calculates (I have this block of code written too). My formula works, but I'm not sure how to get all the values stored or how to let the user go back and pick a choice from the list after he or she has chosen one already.

    Here's what I have so far, and it's a work in progress, but hopefully it's on the right track. I'm thinking I need to, maybe, use a switch statement for the menu, but I can't get that to work for me, and I'm still not sure how to store a value that's not going to be immediately used (maybe something with an array, but again I seem unable to produce the right code).

  2. jcsd
  3. Nov 7, 2006 #2
    I'm not sure what you mean by "store a value to use it later"... any value you store can be used later within that scope. And if you wish to use it another scope, it can be passed by value or reference to a alternative scope.

    You seem to be all over the place with what you want. You should try to break up the program in to separate functions. I'd suggest writing how you think the program should work in pseudo-code; lay out the design of the code in a human-readable syntax. That English syntax, or pseudo-code is an intermediate step before the near-direct translation to C++ code. Perhaps that can help. I see no need for arrays, or anything of that sort.

    Also, I've caught somewhat of the sign that you might think functions are only for the purpose of receiving a unique value for each (set of) input(s), as it applies to Mathematics. However, functions, as they apply to programming, can be merely a "subroutine", or a task that you have solitarily secluded from the main function.

    Additionally, if you could supply greater detail about the functionality of the program, perhaps I could help you with the pseudo-code process of writing the program.
  4. Nov 8, 2006 #3


    User Avatar

    Staff: Mentor

    I suggest that you first (before you write any code) write out an example of what you might see on the screen after running the program through a complete sequence of data input, calculation and output. That is, imagine that after you've taken the program through its paces, you take a screenshot or copy the contents of the terminal window into a text file. This will help you focus your thinking and maybe give you come clues as to the structure of the program.
  5. Nov 8, 2006 #4
    Yes, there you go cworrier, two people who vouch that you plan out your design first. It'll help give you a better direction.

    To give you an idea of how important this approach to programming is: I'm currently writing a simple program for a school assignment. The goal of the assignment was to work in groups and evenly distribute the workload so everyone is programming an equal amount of code. In order to do this effectively, and synchronize it all so that there would be no code left out, we had to make a design plan of all the necessary functions. This is also necessary to assert that there will be no code left out in the end when it's all put together.

    This sort of writeup looks like:

    Code (Text):
    1)   int GCD (int a, int b);

      Return the greatest common denominator of a and b.
      Example: (6, 9) -> (3)

    2)   void reduce (myFract *fractType);
      Reduce the fraction myFract. Do this by using the function GCD.
      Example: (6/9) -> (2/3)

    3)   int LCM (int a, int b);

      Return the lowest common denominator of a and b.
      Example: (6, 9) -> (36)
    Etc... Then each of us have an idea of what to do. As well, problems in the code can be secluded to a specific area, instead of resonating throughout one bloated main function.

    I realise that for the purposes of this assignment, a "design plan", persay, is pure overkill. But I think its important to weigh your options.
  6. Nov 8, 2006 #5
    Thanks to all of you for you words, and I realize now I should have posted the design plan in with it. I actually figured out how to get what I needed. I was over-thinking the whole thing and all I really needed to do was include my menu and the switch statements I developed in a do-while loop to get them to loop forever unless the user chose to exit.

    As for storing values, I wasn't sure how I was going to pull up the values the user was storing for the amount, rate, and term, but I realized that's all done when I called on the variables after user input--they aren't reset to 0, they retain the value until the user chooses to reset them.

    Again, thanks, and next time I post, I promise to make the objective more clear.

    BTW, should I post the completed, working code just so everyone can see?
  7. Nov 8, 2006 #6
    That's great! Congratulations on the accomplishment. Good to hear.

    Yes, that was my first impression, that you weren't subjectively approaching the task, and/or over-thinking the problem at hand. Obviously that was resolved.

    I've love to see your code. Remember that code will almost never be perfect, for programming can almost be considered an art where one person can prefer a certain aspect over an other person's preferences.

    I could help, if you'd like, constructively criticise your code and make sure you aren't doing anything funny. The best way to learn is to finish a task, and then perfect it.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: C++ program not working, need help