please help with a do while loop question

I created this program to all the specs that our teacher required except for having it loop to allow the user to enter salaries until the sentinel value is entered. I have tried everything with no luck. Any help anyone can give me would be greatly appreciated. I know that there needs to be another cout and cin statement in the outer loop but I can’t make it work.


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
int salary = 0;
double rate = .03; //counter
double raise = 0.0;


cout << "Enter Salary Amount(-1 to stop): ";
cin >> salary;

do //begin loop
{
for (int years = 1; years < 4; years += 1)
{
raise = salary * pow(1 + rate, years);
cout << "Year " << years << ":" << endl;
cout << fixed << setprecision(0);
cout << " Rate " << rate * 100 << "%: $";
cout << fixed << setprecision(2) << raise << endl << endl;
} // end for

//update rate counter
rate += .01;
} while (rate < .07);

return 0;
} //end of main
I`m not sure I understand. Does this mean you are not allowed to use the for or while loops? If this is the case have you considered the switch()/case : construct?
I'm sorry let me clarify a little. We are studying posttest loops and the assignment was to create a program that when the user enters a salary it displays raises of 3, 4, 5, and 6% for the next 3 years. ( It displays all the percentages for the first year, and then the second, and finally the third. I got that far, but the program is supposed to loop back around and allow the user to enter as many salaries as they want until they enter a sentinel value. Up to now I've not had any trouble looping other programs but for this one I'm just stuck. I'm sure it's something simple that needs to be done, but I can't make it work. All I really need is to make it loop back and allow the user to enter as many salaries as they want.
closed account (zwA4jE8b)
first:
you need
1
2
cout << "Enter Salary Amount(-1 to stop): ";
 cin >> salary;

in the loop if you want the user to enter a new salary.

second:
your while statement needs to check for whatever sentinel condition you want.

example
1
2
3
4
5
6
do
{
   //.....code here
   std::cout << "Do you want to go again ( y / n)?";
   std::cin >> choice;
}while(choice == 'y' || choice == 'Y')


EDIT:
for mine I used the char 'y' or 'Y'. I just noticed you have '-1' for an exit. that is fine too.
Last edited on
First let me say thank you all for taking the time to try and help. I appreciate it. Where exactly in the outer loop should the second cout, cin statement go? I know that usually it's at the end but I have not been able to make it work. I have tried several different things but it just never has worked right. I've had it to loop but after you enter the salary the program stops and i've had it to try and loop through again but it would only display 1 year and it had incremented to 7%. It's my first use of a do while loop and that's whats throwing me because the do loop is ending with the matching while loop and i'm just confused on where to put the second cout, cin statement.
closed account (zwA4jE8b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "Enter Salary Amount(-1 to stop): "; //Needs to be inside do-while loop
 cin >> salary; //needs to be inside do-while loop

 do //begin loop
 {
 for (int years = 1; years < 4; years += 1)
 {
 raise = salary * pow(1 + rate, years);
 cout << "Year " << years << ":" << endl;
 cout << fixed << setprecision(0);
 cout << " Rate " << rate * 100 << "%: $";
 cout << fixed << setprecision(2) << raise << endl << endl;
 } // end for

 //update rate counter
 rate += .01;

  //here ask if user wants to go again and input their choice
 } while (rate < .07); //needs to check for sentinel condition. NOT <.07. ** 


Your for loop is in the correct place but if you want to run from 3% to 7% you need something more like...

1
2
3
4
5
6
7
8
9
10
11

for (double rate = .03; rate < .07; rate+=.01)
  for (int years = 1; years < 4; years += 1)
 {
   raise = salary * pow(1 + rate, years);
   cout << "Year " << years << ":" << endl;
   cout << fixed << setprecision(0);
   cout << " Rate " << rate * 100 << "%: $";
   cout << fixed << setprecision(2) << raise << endl << endl;
 } // end for


if you do it this way, then you need to remove 'rate' from everywhere else
Last edited on
Have you gotten into functions yet?

If so then
Create a function to add a double value to rate and have it return the new rate.
The rest is up to you.
Else
Move:
1
2
cout << "Enter Salary Amount(-1 to stop): ";
        cin >> salary;

like CreativeMFS said
To the loop
If you don't know which one,
try a little bit of trial and error, there are only 2 loops for it to go in.
else
Your set
fi
Thank you guys so much, I took a lot of your advice and now the program works perfectly and I learned a lot in the process, which is the point of going to school. I never expect anyone to do my work for me, but sometimes we all might need a little help and I really appreciate it.
Last edited on
Topic archived. No new replies allowed.