Help with else and if statements

My error message is not popping up on the last else if statement although I am inputting a negative number.
What I am trying to get it do is that if a negative number is inputted in, the program will print the error message and then continue with the loop where is asks for the input of the name.

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
  #include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
	
	double initial_amount;
	int years_wished, compound_yearly;
	string full_name, XXXX;
	cout << " Program Number 2" << endl;
	cout << " Joe Blake" << endl;
	cout << " Computer Science 1" << endl << endl;
	
	do
{
	cout << "Enter your name or XXXX to stop: ";
	getline(cin, full_name);
	if (full_name != "XXXX")
	{
	cout << endl << full_name << ", please enter the following information: " << endl;
	cout << left << setw(5) << " " << "Amount on deposit: "<< right << setw(45) << "$";
	cin >> initial_amount;
	if (!cin || initial_amount < 0)
	{
		cout << "Error: Negative number for deposit, try again."<< endl;
	}
	cout << left << setw(5) << " " << "Years on deposit: "<< right << setw(45) <<" ";
	cin >> years_wished;
	cout << left << setw(5) << " " << "Number of times the interest is compounded per year: " << right << setw(10) << " ";
	cin >> compound_yearly;
	if (years_wished > 0 || compound_yearly > 0)
	{
				double interestrate, final_amount, interest_earned;
				if (years_wished >= 5)
					interestrate = .045;
				else if (years_wished >= 4)
					interestrate = .04;
				else if (years_wished >= 3)
					interestrate = .035;
				else if (years_wished >= 2)
					interestrate = .025;
				else if (years_wished >= 1)
					interestrate = .02;
				else
					interestrate = .015;
				cout << showpoint << fixed << setprecision(2);
				final_amount = initial_amount * pow((double)(1 + (interestrate / compound_yearly)), (compound_yearly*years_wished));
				interest_earned = final_amount - initial_amount;
				cout << left << setw(15) << "\n\nName" << setw(10) << "Years" << setw(20) << "Deposit Amount" << setw(20) << "Interest Earned" << " Total" << endl;
				cout << setfill('-') << setw(73) << "" << setfill(' ') << endl;
				cout << setfill('-') << setw(73) << "" << setfill(' ') << endl;
				cout << left << setw(15) << full_name << setw(8) << years_wished << "$" << setw(19) << initial_amount << "$" << setw(20) << interest_earned << "$" << final_amount << endl << endl;
				cin.ignore();
			}
	}
	else if (years_wished < 0 || compound_yearly < 0)
	{ cout << "Error: Negative number for years, try again." << endl; 
	}
}
while(full_name != "XXXX");
	cout << "Thank you for using tax program. Have a nice day." << endl << endl;
	return 0;
}
Are you freaking kidding me? I gave you a working program that does exactly what you say your current program is not doing, in your last post, less than an hour ago. Go read the code I gave you.
My bad. I guess I wasn't so clear with you on what I want it to do. What I am trying to get it do is that if a negative number is inputted in, the program will print the error message and then continue with the loop where it asks for the input of the name. I do not want it to calculate the interest or any of the numbers when a negative number is inputted in. I don't want it to give me the table if a negative number is inputted. I just want the error message and then for the loop to start again to where it asks for the name.
1
2
3
4
if (condition < 0)
{
cout << "Error";
}
I have that already.
closed account (SECMoG1T)

1
2
3
4
5
6
7
8
9
if (years_wished > 0 || compound_yearly > 0)
//change to
if (years_wished > 0 && compound_yearly > 0)

///and

else if (years_wished < 0 || compound_yearly < 0)
//to
else


here

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
if (years_wished > 0 && compound_yearly > 0)
	{
				double interestrate, final_amount, interest_earned;
				if (years_wished >= 5)
					interestrate = .045;
				else if (years_wished >= 4)
					interestrate = .04;
				else if (years_wished >= 3)
					interestrate = .035;
				else if (years_wished >= 2)
					interestrate = .025;
				else if (years_wished >= 1)
					interestrate = .02;
				else
					interestrate = .015;
				cout << showpoint << fixed << setprecision(2);
				final_amount = initial_amount * pow((double)(1 + (interestrate / compound_yearly)), (compound_yearly*years_wished));
				interest_earned = final_amount - initial_amount;
				cout << left << setw(15) << "\n\nName" << setw(10) << "Years" << setw(20) << "Deposit Amount" << setw(20) << "Interest Earned" << " Total" << endl;
				cout << setfill('-') << setw(73) << "" << setfill(' ') << endl;
				cout << setfill('-') << setw(73) << "" << setfill(' ') << endl;
				cout << left << setw(15) << full_name << setw(8) << years_wished << "$" << setw(19) << initial_amount << "$" << setw(20) << interest_earned << "$" << final_amount << endl << endl;
				cin.ignore();
			
	}
	else
	{ 
              cout << "Error: Negative number for years, try again." << endl; 
	}


that would do.
Last edited on
Thanks man but I figured out that I needed a cin.get after my error message. And it would make it run perfectly.
Topic archived. No new replies allowed.