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

  • C/C++
  • Thread starter cworrier
  • Start date
  • Tags
    C++ Program
In summary: Then we share them all and put them together. It's worked great for us. I just have to finish writing the main function and I'm done. All of this work was done without any of us ever meeting in person or calling each other on the phone. It's all been over email and the occasional IM.
  • #1
cworrier
3
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
  • #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 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.
 
  • #3
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.
 
  • #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:
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.
 
  • #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?
 
  • #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 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.
 

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

1. Why is my C++ program not compiling?

There could be several reasons for this. Some common issues include syntax errors, missing header files, or incorrect compiler settings. Make sure to carefully check your code for any errors and ensure that you have all necessary files included. If the issue persists, try resetting your compiler settings or seeking help from a more experienced programmer.

2. My C++ program compiles, but it doesn't run. What's wrong?

This could be due to logical errors in your code. Make sure to thoroughly test your program and check for any mistakes in your algorithms or logic. Additionally, check if all necessary libraries are linked and if any input/output operations are functioning correctly.

3. How do I debug my C++ program?

To debug your program, you can use a debugger tool that allows you to step through your code line by line and track the values of variables. You can also add print statements in your code to track the flow of execution and identify any errors. Another helpful technique is to use online resources or forums to seek help from other programmers who may have encountered similar issues.

4. My C++ program was working before, but now it suddenly stopped working. What happened?

This could be due to changes in your system environment or updates to your compiler. It's also possible that your code was accidentally modified or that there are external factors affecting your program's functionality. Try to identify any recent changes that may have caused the issue and revert them if possible. Otherwise, carefully review your code for any unintentional changes and try to debug the problem.

5. I keep getting errors when trying to compile my C++ program. How can I fix them?

The first step is to carefully read and understand the error messages. They often contain helpful information about the issue and can guide you towards a solution. If you're still stuck, try searching online for the specific error message or seeking help from other programmers. It's also important to regularly save and back up your code, so you can revert to a previous working version if needed.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
10
Views
966
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
894
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
Back
Top