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

  • Context: C/C++ 
  • Thread starter Thread starter cworrier
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary

Discussion Overview

The discussion revolves around how to store and retrieve user input in a C++ program, specifically in the context of a mortgage calculation application. Participants explore various programming concepts, including menu-driven interfaces, data storage, and function design.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in storing user choices for later use and suggests using a switch statement for menu navigation.
  • Another participant questions the need for arrays, suggesting that values can be passed between scopes and recommending breaking the program into functions.
  • A suggestion is made to write out an example of the program's output to clarify the design and structure before coding.
  • Participants emphasize the importance of planning the program's design to avoid confusion and ensure all necessary functions are included.
  • The original poster later realizes that using a do-while loop with switch statements can help manage user input effectively and that variables retain their values until reset.
  • There is an offer for constructive criticism of the code once it is completed, highlighting that programming can be subjective and open to personal preferences.

Areas of Agreement / Disagreement

Participants generally agree on the importance of planning and structuring the program before coding. However, there are differing views on the necessity of certain programming constructs, such as arrays and functions, indicating that multiple approaches are considered valid.

Contextual Notes

Some participants note that the original poster may have over-complicated the problem, but the discussion does not resolve the broader questions about the best practices for storing and retrieving user input in C++.

Who May Find This Useful

Readers interested in C++ programming, particularly those working on user input handling, menu-driven applications, and function design in programming projects.

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;

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

count << " \n";

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

count << " \n";

count << "You Entered" << endl;
count << "Loan Amount: $" << a << endl;
count << "Loan Rate: " << b << "%" << endl;
count << "Loan Term: " << c << "years" << endl;
count << "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.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 8 ·
Replies
8
Views
5K
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
5K
Replies
10
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
2
Views
2K