Target money: $20
Day 0 you save $0 so you have $0
Day 1 you save $1 so you have $1
Day 2 you save $2 so you have $3
Day 3 you save $3 so you have $6
Day 4 you save $4 so you have $10
Day 5 you save $5 so you have $15
Day 6 you save $6 so you have $21
The amount of money you save is the same as the day number. When I enter the target money, I need the program lists out the saving progress from day 0 until reaching the saving target.
#include <iostream>
usingnamespace std;
int main()
{
int tm;
cout << "Target money: ";
cin >> tm;
// first loop
for (int i = 0; i >=0 ; i++)
{
// second loop
for (int j = i; j >=0; j++)
{
int k;
for (k = j+i ; k >= j + i; k++)
if (k >= tm)
{
break;
}
cout << "Day " << i << " you save $" << j << " so you have $" << k << endl;
}
}
return 0;
}
I tried this but I think I do it so bad, so I didn't post it.
Does anyone know how to print this? Yes, so what have you done?
keskiverto once wrote:
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.
We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't make and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
Line 15 is still an endless for loop. Think about what you have and you will see that "i" will always be ">=" to (0) zero. Also it does not work because you never get back to it to change the value of "i" when it needs to be changed because the 2nd for loop is also an endless loop.
The 3rd for loop does not make any sense. It seems to be over kill for what should just be an if statement.
I just ran the code for 3 lines, but this was my output:
Target money: 20
Day 0 you save $0 so you have $20
Day 0 you save $1 so you have $20
Day 0 you save $2 so you have $20
This shows me that the "Day" never changes and the amount of money you have should start at (0) zero not 20.
I have not worked on it yet, but I am thinking that 1 for loop should do the trick.
#include <iostream>
usingnamespace std;
int main()
{
int tm;
cout << "Target money: ";
cin >> tm;
for (int i = 0; i < i+1 ; i++)
{
for (int k = 0; k >= k + i; k++)
{
if (k >= tm)
{
break;
}
cout << "Day " << i+k << " you save $" << i+k << " so you have $" << k+i << endl;
}
}
return 0;
}
output
Target money: 20
Day 0 you save $0 so you have $0
Day 1 you save $1 so you have $1
Day 2 you save $2 so you have $2
Day 3 you save $3 so you have $3
Day 4 you save $4 so you have $4
Day 5 you save $5 so you have $5
Day 6 you save $6 so you have $6
Day 7 you save $7 so you have $7
Day 8 you save $8 so you have $8
Day 9 you save $9 so you have $9
Day 10 you save $10 so you have $10
Day 11 you save $11 so you have $11
Day 12 you save $12 so you have $12
Day 13 you save $13 so you have $13
Day 14 you save $14 so you have $14
Day 15 you save $15 so you have $15
Day 16 you save $16 so you have $16
Day 17 you save $17 so you have $17
Day 18 you save $18 so you have $18
Day 19 you save $19 so you have $19
// Example program
#include <iostream>
int main()
{
int target = 20;
std::cout << "Target money: $" << target << '\n';
for (int n = 0; ; n++)
{
int total = (n*(n+1)/2);
std::cout << "Day " << n << " you save $" << n << " so you have $" << total << '\n';
if (total >= target)
{
break;
}
}
}
Target money: $20
Day 0 you save $0 so you have $0
Day 1 you save $1 so you have $1
Day 2 you save $2 so you have $3
Day 3 you save $3 so you have $6
Day 4 you save $4 so you have $10
Day 5 you save $5 so you have $15
Day 6 you save $6 so you have $21
#include <iostream>
usingnamespace std; // <--- Best not to use.
int main()
{
constexprint TOTALDAYS{ 10 };
int targetMoney{}, totalSaved{};
//cout << "Target money: ";
cout << "Enter your savings target: ";
cin >> targetMoney;
for (int day = 0; day < TOTALDAYS; day++)
{
totalSaved += day;
cout << "Day " << day << " you save $" << day << " so you have $" << totalSaved << '\n';
if (totalSaved >= targetMoney)
{
std::cout << "\n In " << day << (day > 1 ? " days" : " day") << " you have reached your target.\n";
break;
}
}
return 0; // <--- Not required, but makes a good break point.
}
#include <iostream>
usingnamespace std;
int main()
{
int targetMoney {}, days {};
cout << "Enter your savings target: ";
cin >> targetMoney;
for (int totalSaved {}; totalSaved < targetMoney + days; totalSaved += ++days)
cout << "Day " << days << " you save $" << days << " so you have $" << totalSaved << '\n';
std::cout << "\nIn " << days - 1 << " days you have reached your target.\n";
}
Enter your savings target: 100
Day 0 you save $0 so you have $0
Day 1 you save $1 so you have $1
Day 2 you save $2 so you have $3
Day 3 you save $3 so you have $6
Day 4 you save $4 so you have $10
Day 5 you save $5 so you have $15
Day 6 you save $6 so you have $21
Day 7 you save $7 so you have $28
Day 8 you save $8 so you have $36
Day 9 you save $9 so you have $45
Day 10 you save $10 so you have $55
Day 11 you save $11 so you have $66
Day 12 you save $12 so you have $78
Day 13 you save $13 so you have $91
Day 14 you save $14 so you have $105
In 14 days you have reached your target.