Morgage code will not work!

Soo, I am not good at this. I keep starting over and now its a mess. I do not understand why when I ask for the principal amount it allows two responses. And then it doesn't ask the credit score..then I can not figure out how to make the numbers work within a perimeter. loan amount only between $0-800,000. I got it to not allow letters in int. but can't get the rest.
Need:1. Ask for an amount(principle):
(can not be a crazy amount or neg #)
2. Ask the credit score which I have a table excellent-720-850(rate=2.75%-4.0%)
Good-690-719(4.01%-6.50)Fair-630-689(rate 6.51%-8.75)Bad-300-629(rate 8.76%-10.50%).
3. The interest rate has to be calculated with a rand # withing the range. using Enum
4.then calculate payments for 12 months.


that's it!! but I can not seem to figure out the beginning.





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  //  Week 1 


#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

int main()
{
	double principle = 0;
	char again = 'y';
	int creditScore;
	int loanLength;
	bool P = false;

	while (again == 'y' )
	{
		//Get the amount of principal wanted 
		cout << "Type in the principal amount:\n";
		while (!(cin >> principle)) {
			while (!(principle)); {
				if (principle <= 100000 && principle > 0) {
					P = true;
				}
				else
				{
					cout << "Pick a number between 0 - 100000:\n";
					cin >> principle;
				}
			}
			cout << "Must be a number:\n";
			cin.clear();
			cin.ignore(100, '\n');



		
		}
		if (principle == 0)
			//Assign credit score 
			cout << "What is your credit score?:\n";
		while (!(cin >> creditScore)) {
			cout << "Must be a number:\n";
			cin.clear();
			cin.ignore(100, '\n');



		}
		cout << "What length of a loan do you want?:10,15 or 30\n";
		while (!(cin >> loanLength)) {
			cout << "Must be a number 10,15 or 30:\n";
			cin.clear();
			cin.ignore(100,'\n');
		} {

			
				cout << "Congratulations! You are on the path to getting a great loan!\n";
			
		}
	}
	
	return 0;

}
Here's your code with clearer indentation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

int main()
{
  double principle = 0;
  char again = 'y';
  int creditScore;
  int loanLength;
  bool P = false;

  while (again == 'y') {
    //Get the amount of principal wanted
    cout << "Type in the principal amount:\n";
    while (!(cin >> principle)) {
      while (!(principle));
      {
        if (principle <= 100000 && principle > 0) {
          P = true;
        } else {
          cout << "Pick a number between 0 - 100000:\n";
          cin >> principle;
        }
      }
      cout << "Must be a number:\n";
      cin.clear();
      cin.ignore(100, '\n');
    }

    if (principle == 0)
      //Assign credit score
      cout << "What is your credit score?:\n";

    while (!(cin >> creditScore)) {
      cout << "Must be a number:\n";
      cin.clear();
      cin.ignore(100, '\n');
    }

    cout << "What length of a loan do you want?:10,15 or 30\n";
    while (!(cin >> loanLength)) {
      cout << "Must be a number 10,15 or 30:\n";
      cin.clear();
      cin.ignore(100, '\n');
    }

    {
      cout << "Congratulations! You are on the path to getting a great loan!\n";
    }
  }

  return 0;
}


1. The while loop at line 19 will either do nothing at all, or do nothing forever.
That ; at the end is a killer.

2. You don't prompt for credit score because of the if on line 33. This can never be true because of line 21.
Thanks!
How do you control the number of answers that can be given back?
How can I isolate the 10, 15 and 30?
Like
if ( loanLength == 10 || loanLength == 15 || loanLength == 30 )
How, do I only prompt one question and not allow two entries for my questions?
I do not understand your question, but this

1
2
3
4
5
6
7
8
{
        if (principle <= 100000 && principle > 0) {
          P = true;
        } else {
          cout << "Pick a number between 0 - 100000:\n";
          cin >> principle;
        }
      }


seems wrong. I think you meant this:
1
2
3
4
5
do
{
      cout << "Pick a number between 0 - 100000:\n";
       cin >> principle;    
} while (principle > 100000 || principle < 0)


your code sets P but checks principle, and it only checks if its zero or not. You can fix yours by putting P into the while loop, but its cleaner with the do-while approach and no P at all.
Last edited on
Make one loop work first, then add a second and third loop when you understand what's going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
  double principal = 0;

  cout << "Type in the principal amount (<100000):\n";
  // While there is input failure, or it's out of range
  while( !(cin >> principal) || principal < 0 || principal > 100000 ) {
      cout << "Pick a number between 0 - 100000:\n";
      cin.clear();
      cin.ignore(100, '\n');
  }

  cout << "principal=" << principal << endl;
}


Becomes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
  double principal = 0;
  int creditScore;

  cout << "Type in the principal amount (<100000):\n";
  // While there is input failure, or it's out of range
  while( !(cin >> principal) || principal < 0 || principal > 100000 ) {
      cout << "Pick a number between 0 - 100000:\n";
      cin.clear();
      cin.ignore(100, '\n');
  }

  cout << "principal=" << principal << endl;

  cout << "What is your credit score?:\n";
  while( !(cin >> creditScore) || creditScore < 0 || creditScore > 1000 ) {
      cout << "Pick a number between 0 - 1000:\n";
      cin.clear();
      cin.ignore(100, '\n');
  }

  cout << "creditScore=" << creditScore << endl;
}


Example test run.
$ ./a.out 
Type in the principal amount (<100000):
banana
Pick a number between 0 - 100000:
-22
Pick a number between 0 - 100000:
10000000
Pick a number between 0 - 100000:
1234
principal=1234
What is your credit score?:
999
creditScore=999



When you've accumulated a bit of useful functionality, then make functions out of it to keep any one function from becoming too cluttered.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<iomanip>
using namespace std;

double getPrincipal() {
  double principal = 0;
  cout << "Type in the principal amount (<100000):\n";
  // While there is input failure, or it's out of range
  while( !(cin >> principal) || principal < 0 || principal > 100000 ) {
      cout << "Pick a number between 0 - 100000:\n";
      cin.clear();
      cin.ignore(100, '\n');
  }
  return principal;
}

int getCreditScore() {
  int creditScore;
  cout << "What is your credit score?:\n";
  while( !(cin >> creditScore) || creditScore < 0 || creditScore > 1000 ) {
      cout << "Pick a number between 0 - 1000:\n";
      cin.clear();
      cin.ignore(100, '\n');
  }
  return creditScore;
}

int main()
{
  double principal = getPrincipal();
  cout << "principal=" << principal << endl;

  int creditScore = getCreditScore();
  cout << "creditScore=" << creditScore << endl;
}

Registered users can post here. Sign in or register to post.