Mar 19, 2015 at 7:24pm UTC
I need help creating a loop that when the user enters XXXX it would be terminated. The problem with the following loop is that after the first attempt of inputting the full_name, it no longer asks me what my name is.
Code:
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
#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;
cout << "Enter your name or XXXX to stop: " ;
getline(cin, full_name);
while ( (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)
{
cout << "Error: Negative number for years, try again." << endl;
}
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;
}
cout << "Thank you for using tax program. Have a nice day." << endl << endl;
return 0;
}
Last edited on Mar 19, 2015 at 8:07pm UTC
Mar 19, 2015 at 7:45pm UTC
Thank you so much! I am just trying to figure out now where to put some of the coding so that it would terminate sooner so that it would stop right after inputting XXXX
Mar 19, 2015 at 7:53pm UTC
Yeh I looked back and some of the info I gave you were not right. Im working on your program and will be updating this post. For now, Move
cin.ignore();
to the very last line before the loop ends.
1 2
cin.ignore();
} while (full_name != "XXXX" );
Edit:
I am just trying to figure out now where to put some of the coding so that it would terminate sooner so that it would stop right after inputting XXXX
You can solve this multiple ways, using bools etc. But you could just use an if statement.
After
1 2
cout << "Enter your name or XXXX to stop: " ;
getline(cin, full_name);
Put the if statement -
if (full_name != "XXXX" )
And end it right above the loop just after the cin.ignore();
1 2 3
cin.ignore();
} // ending if statement
} while (full_name != "XXXX" );
Last edited on Mar 19, 2015 at 7:57pm UTC
Mar 19, 2015 at 8:20pm UTC
Thank you! You're a life saver!
Mar 19, 2015 at 8:22pm UTC
You're very welcome.
Happy Coding!
Mar 19, 2015 at 8:29pm UTC
If its not too much. I have another question. On line 34 I have an if statement to check for negative numbers. What I want to happen is that if a negative number is inputted it would give the error message on line 36 and skip the rest of the coding to continue all over again in the loop. An "exit (0)" would not work because it would terminate the loop. Is there another code I can use to do such thing?
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
#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)
{
cout << "Error: Negative number for years, try again." << endl << endl << endl;
continue ;
}
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 << endl << endl;
cin.ignore();
}
}
while (full_name != "XXXX" );
cout << "Thank you for using tax program. Have a nice day." << endl << endl;
return 0;
}
Last edited on Mar 19, 2015 at 8:39pm UTC
Mar 19, 2015 at 8:42pm UTC
Never mind I got it to work by moving it to after line 60. Thank you!
Mar 19, 2015 at 8:53pm UTC
That is not working for some reason.
Mar 19, 2015 at 8:54pm UTC
Works fine for me. Please post your entire code so I can see what you've done wrong.
Also, be more specific. Do you want it to go back to the name changing if a negative number is entered, or back to where the user can enter years_wished and compound_yearly again?
Last edited on Mar 19, 2015 at 8:57pm UTC
Mar 19, 2015 at 9:06pm UTC
You have to be more specific. You told me you wanted it to restar the entire loop. Im assuming you want it to go back to where you can re-enter years_wished and compound_yearly. You do it like this instead
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
do
{
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
{
cout << endl << "Error: Negative number for years, try again. \n" << endl;
}
} while (years_wished < 0 || compound_yearly < 0);
}
} while (full_name != "XXXX" );
Last edited on Mar 19, 2015 at 9:06pm UTC