C/C++ How can I store and retrieve user input in a C++ program?

  • Thread starter Thread starter cworrier
  • Start date Start date
  • Tags Tags
    C++ Program
AI Thread Summary
The discussion centers on troubleshooting a C++ program designed for mortgage calculations, where the user selects options from a menu to input loan details. The user seeks guidance on how to store multiple inputs, allow for repeated selections, and implement a clear function and exit option. Initial attempts included using a switch statement and arrays, but confusion arose regarding value storage and program structure. Participants suggest breaking the program into functions, using pseudo-code for planning, and employing a do-while loop for continuous menu access. The user eventually realizes that variables retain their values until reset, leading to a breakthrough in understanding. The conversation emphasizes the importance of clear design and iterative development in programming, with an invitation to share the completed code for further feedback.
cworrier
Messages
3
Reaction score
0
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).

#include <iostream>
#include <math.h>
using namespace std;

doublemortgage (int c, double b, double a)
{
int i = c;
double j = b;
double k = a;
double l = b/(12 * 100);
double m = c * 12;
double result = k * (l / (1 - pow(1+l, -m)));
return result;
}


int main ()
{
int c;
double b;
double a;
double result;
int x;

cout << "Mortgage Calculation Menu \n";
cout << "1. Enter a Loan Amount \n";
cout << "2. Enter a Loan Rate \n";
cout << "3. Enter a Term in Years \n";
cout << "4. Calculate a Payment \n";
cout << "5. Clear All Input \n";
cout << "9. Quit \n";
cout << "Enter Your Selection: ";
cin >> x;

cout << " \n";

cout << "Enter a Loan Amount: $";
cin >> a;
cout << "Enter a Loan Rate: ";
cin >> b;
cout << "Enter a Term in Years: ";
cin >> c;
cout << "Your Monthly Payment: $"<< mortgage(c, b, a) << endl;

cout << " \n";

cout << "You Entered" << endl;
cout << "Loan Amount: $" << a << endl;
cout << "Loan Rate: " << b << "%" << endl;
cout << "Loan Term: " << c << "years" << endl;
cout << "Monthly Payment: $" << mortgage (c, b, a) << endl;


return0;
}
 
Technology news on Phys.org
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 into 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.
 
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.
 
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:
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 realize that for the purposes of this assignment, a "design plan", persay, is pure overkill. But I think its important to weigh your options.
 
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?
 
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 criticize 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top