Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,511 people online right now. Registration is fast and FREE... Join Now!




Returning to Menu Problem

 
Reply to this topicStart new topic

Returning to Menu Problem, C++ program not working well.

cworrier
7 Nov, 2006 - 02:20 PM
Post #1

New D.I.C Head
*

Joined: 7 Nov, 2006
Posts: 4


My Contributions
I've been working on my boyfriend's C++ program since last Sunday and I still can't get it right. I've tried a lot of different solutions and I just can't quite get over one big obstacle.

The problem is this--I need the program to offer the user a selection from a menu, but when I do, I can't get the menu to work again. It will display again, but the choices don't do anything. It's a generic mortgage calculator that I have done in several other languages, but being relatively new to C++, I just can't figure out what I'm doing wrong. My boyfriend, as of yet, has been very little help. I tutored him up until this point, but now I'm stumped too.

The version I'm working with now has a switch statement, because that seems to work best for menus, but I don't know how to bring it back to the menu. My boyfriend keeps insisting that there should be something with an array because that was what he's been studying. I'm not sure about that or how an array would accomplish what I need done.

Here is the code so far, and sorry it's so long.

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;
switch(x)
{
case1: cout << "Enter a Loan Amount: $";
cin >> a;
break;

case2: cout << "Enter a Loan Rate: ";
cin >> b;
break;

case3: cout << "Enter a Term in Years: ";
cin >> c;
break;

case4: 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;
break;

case5: cout << "Data Cleared";
a = 0;
b = 0;
c = 0;
break;

case9: cout << "Quitting Program";
break;

default: cout << "Invalid Selection. Choose Another Option.";
break;
}

return0;
}

User is offlineProfile CardPM
+Quote Post

ZlxA
RE: Returning To Menu Problem
7 Nov, 2006 - 04:32 PM
Post #2

New D.I.C Head
*

Joined: 6 Nov, 2006
Posts: 10


My Contributions
You need to put your switch / menu inside a loop ( for your case I'd say a do while loop), like so:

CODE


do
{
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;
switch(x)
{
case1: cout << "Enter a Loan Amount: $";
cin >> a;
break;

case2: cout << "Enter a Loan Rate: ";
cin >> b;
break;

case3: cout << "Enter a Term in Years: ";
cin >> c;
break;

case4: 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;
break;

case5: cout << "Data Cleared";
a = 0;
b = 0;
c = 0;
break;

case9: cout << "Quitting Program";
break;

default: cout << "Invalid Selection. Choose Another Option.";
break;
}

}while(x ! = 9);


User is offlineProfile CardPM
+Quote Post

cworrier
RE: Returning To Menu Problem
7 Nov, 2006 - 05:03 PM
Post #3

New D.I.C Head
*

Joined: 7 Nov, 2006
Posts: 4


My Contributions
Thank you so much! That was perfect! I guess I was looking for it to be harder than that and turns out it was pretty simple, huh?

Thank you again!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:08PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month