i need help

After level 5 lockdown, Rose, Rebecca and Rachael (three friends) realized that they had gained
much weight. Therefore, they decided to do exercise activities to help them lose at least 10kg in
weight. They all agreed to do one exercise activity every day, which is hiking. You are requested to
write a C++ program that will help them to determine which one of the three friends lost at least 10kg
in weight within 40 days before the other two friends. Your program should perform the following:
• Prompt the user to enter the name and starting weight for each of the three friends.
• Then, for each day, prompt the user their weight for that day.
• Calculate and display the weight loss for that day.
• Also calculate how many days it took one friend to lose the 10kgs within 40 days before
the other two friends.
Last edited on
Exercise is all the help you need.
This is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions. Show us what you've written.
#include<iomanip>
#include<iostream>
#include<string>
#include<cmath>

using namespace std;

int main()
{

int StartingWeight;
string Name;
int count = 1;

while (count <= 3)
{

cout << "Enter the Name" << endl;
cin >> Name;
cout << "Enter the starting weight" << endl;
cin >> StartingWeight;
++count;
}
int day = 1;
int weightlost;
int weightOftheday;
while (day <= 40)
{

cout << "Enter the weight of the day " << " " << day << endl;
cin >> weightOftheday;
day = day + 1;
weightlost = StartingWeight - weightOftheday;
cout << "weight lost of the day is " << weightlost << endl;

}


EXIT_SUCCESS;
}
calculate many days it took one friend to lose the 10kgs within 40 days before the other two friends
As stated, this will require potentially 123 weights to be entered (3 for each of the 40 days plus the 3 initial weights). This really is only feasible if the data is held on a file. I don't fancy entering all that data using the keyboard for every test!

Have you covered arrays?
Last edited on
i haven't covered arrays
As a starter, perhaps consider:

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
#include <string>
#include <iostream>
#include <iomanip>
#include <limits>

constexpr unsigned noFrnds {3};
constexpr unsigned noDays {4};
constexpr double targ {10};

struct Data {
	std::string name;
	double init {};
	double prev {};
};

template<typename T = int>
auto getNum(const std::string& prm)
{
	const auto notsp {[&]() {while (std::isspace(static_cast<unsigned char>(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }};
	T n {};

	while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
		std::cout << "Not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return n;
}

int main()
{
	Data data[noFrnds] {};

	for (unsigned f = 0; f < noFrnds; ++f) {
		std::cout << "Enter name of friend " << f + 1 << ": ";
		std::getline(std::cin >> std::ws, data[f].name);

		data[f].init = getNum<double>("Enter start weight: ");
		data[f].prev = data[f].init;
	}

	for (unsigned d = 0; d < noDays; ++d) {
		std::cout << "Day " << d + 1 << '\n';

		for (unsigned f = 0; f < noFrnds; ++f) {
			if (data[f].init - data[f].prev < targ) {
				const auto wght {getNum<double>("Enter weight for friend " + data[f].name + ": ")};

				std::cout << "Their daily weight loss (-ve for gain!) is: " << data[f].prev - wght << '\n';
				data[f].prev = wght;

				if (data[f].init - wght >= targ)
					std::cout << "Congrats! You have lost the target weight in " << d + 1 << " days\n";
			}
		}
	}
}

this codes are a bit difficult to understand
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Referring to your code post:
Line 19, 21: Each time through the loop you overwrite the same variable. When you exit the loop you will have only the third Name and Startingweight. If you haven't learned arrays, it's time to do so.
https://www.cplusplus.com/doc/tutorial/arrays/

Lines 27-36: You ask for the weight of only one person. You need to do this for each of the three people. Again, you need to put the information in an array. You also do not check for which person lost the 10kg first.

Line 39 needs the keyword return

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
#include<iomanip>
#include<iostream>
#include<string>
#include<cmath>

using namespace std;
constexpr int MAX_USERS = 3;
constexpr int MAX_DAYS = 40;

int main()
{
	int weight[MAX_USERS][MAX_DAYS];
	string name[MAX_USERS];
	int count = 0;

	while (count <= MAX_USERS)
	{
		cout << "Enter the Name #" << count << ": "; 
		cin >> name[count];
		cout << "Enter the starting weight for " << name[count] << ": "; 
		cin >> weight[count][0];
		++count;
	}
	int day = 1;
	int weightlost;	
	bool goal_reached = false;
	while (day < MAX_DAYS)
	{	count = 0;
		while (count < MAX_USERS)
		{	cout << "Enter the weight of " << name[count] << " on day #" << day << ": ";
			cin >> weight[count][day];
			weightlost = weight[count][0] - weight[count][day];
			cout << name[count] << "lost " << weightlost << " kg on day #:" << day << endl;
			if (weightlost > 10 && !goal_reached)
			{	cout << name[count] << " reached 10kg on day #" << day << endl;
				goal_reached = true;
			}
			count++;
		}
		day++;
	}

	return EXIT_SUCCESS;
}


Last edited on
Topic archived. No new replies allowed.