What's Missing?

Having issues with the code below. Everything is working fine except the adding function of sum += weight. The program adds/subtracts the last number entered by the user. See below my 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
# include <iostream> 
# include <iomanip>

using namespace std;

int main() {

	int sum = 0, weight = 0;
	int totalOver;

	cout << "This elevator can fit a total of 5 people." << endl;
	cout << "Not to exceed 2500 pounds." << endl;

	for (int i = 0; i < 5; i++)
	{
		cout << "Enter weight: ";
		cin >> weight;
		sum += weight;
	}

	totalOver = weight - 2500;

	if (sum <= 2500)
	{
		cout << "Let's go!" << weight << endl;
	}
	else
	{
		cout << "This elevator is too heavy by " << totalOver << " pounds." << endl;
	}

	return 0;
}



Thank you in advance!
Last edited on
Hello ViyuPr87,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting and line spacing it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

*** I found the second link to be the most help. ***

Revised 08/16/2021


A few changes to show you what you can do:
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
# include <iostream>
# include <iomanip>

using namespace std;  // <--- Best not to use.

int main()
{
    constexpr int MAXWEIGHT{ 2500 };

    int sum = 0, weight = 0;
    int totalOver{};  // <--- ALWAYS initialize all your variables.

    cout <<
        "\n"
        "This elevator can fit a total of 5 people.\n"
        "Not to exceed 2500 pounds.\n\n";

    for (int i = 0; i < 5; i++)
    {
        cout << "Enter weight: ";
        cin >> weight;

        sum += weight;
    }

    totalOver = weight - MAXWEIGHT;

    if (sum <= MAXWEIGHT)
    {
        cout << "\nLet's go!" << weight << '\n';
    }
    else
    {
        cout << "\n     This elevator is too heavy by " << totalOver << " pounds." << '\n';
    }

    return 0;  // <--- Not required, but makes a good break point for testing.
}

When you reach line 26 what is the value of "weight"? And do you think this is the correct variable to be using?

Lines 14 - 16 demonstrate that you do not need a "cout" and "endl" for each line. The 3 lines of quoted strings is considered as just 1 string. Anything more than that can be chained together with the insertion operator(<<).

Other little things are just for output and only a suggestion.

Andy
totalover is wrong. it looks like it should be sum - 2500.

also use code tags (<> on the side editor ) for code; it not only lets us read it easier but you can run it here too.
Last edited on
Hello all!

Thank you for the feedback so far. This is week two of learning and I'm trying to absorb as much as I can. I have added the code tag to the original post. However, the adding portion in line 23 is still not working. It only registers the last input. The subtraction portion in line 26 is working, however, it subtracts the last user input minus 2500.

Standing by.
re-read my post and you can fix the issue.
weight is the last value entered.
sum is the running total.
I saw my mistake!

Thank you, guys!

new code below

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

using namespace std;

int main() {

	int sum = 0, weight = 0;
	int totalOver;

	cout << "This elevator can fit a total of 5 people." << endl;
	cout << "Not to exceed 2500 pounds." << endl;

	for (int i = 0; i < 5; i++)
	{
		cout << "Enter weight: ";
		cin >> weight;
		sum += weight;
	}

	totalOver = sum - 2500;

	if (sum <= 2500)
	{
		cout << "Let's go!" << sum << endl;
	}
	else
	{
		cout << "This elevator is too heavy by " << totalOver << " pounds." << endl;
	}

	return 0;
}
Last edited on
Hi,

You didn't initialise totalOver on line 9. Failing to initialise can cause problem for young and old, always initialise ! :+)

The best thing to do, is declare and initialise when you have a sane value. So delete line 9, and change line 21 to:

int totalOver = sum - 2500; // declaration and initialisation in 1 statement

Variables can be initialised with braces, like this:

1
2
int sum{}; // declare 1 variable per line is good practise
int weight{};


The good thing about this is that the compiler will complain if there is narrowing of a type because information is being lost. For example If a variable is of type int, and one initialises with a long int, the compiler will complain because long int is wider than int. Likewise with a double value to initialise a float.
my 2 cents, you only use totalover once.
put the sum-2500 in the cout:
cout << "words " << sum-2500;

I admit to thinking in extra variable mode as I write, but cleanup pass, I pull the useless ones back out.
Last edited on
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# include <iostream>

int main() {
	constexpr int maxpeople {5};
	constexpr int maxweight {2500};
	int sum {};

	std::cout << "This elevator can fit a total of " << maxpeople << " people.\n";
	std::cout << "Not to exceed " << maxweight << " pounds.\n";

	for (int i {}, weight {}; i < maxpeople; ++i, sum += weight)
		std::cout << "Enter weight for person " << i + 1 << ": ", std::cin >> weight;

	if (sum <= maxweight)
		std::cout << "Let's go! " << sum << '\n';
	else
		std::cout << "This elevator is too heavy by " << sum - maxweight << " pounds\n";
}

@seeplus,

Don't you know how confusing that is for a beginner? Even as a programmer with decades of experience, I have to stop and figure out what lines 11 and 12 are doing. In my opinion, clarity is much more important than terseness.

@ViyuPr87,

You are obviously being taught the Allman style of indentation and braces usage. Not everyone on this forum shuns braces on single-line code blocks. Here's my version:


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
# include <iostream>

int main()
{
    const int maxpeople = 5;
    const int maxweight = 2500;

    std::cout << "This elevator can fit a total of " << maxpeople << " people.\n";
    std::cout << "Not to exceed " << maxweight << " pounds.\n";

    int sum = 0;
    for (int i = 0; i < maxpeople; ++i)
    {
        std::cout << "Enter weight for person " << i + 1 << ": ";

        int weight;
        std::cin >> weight;
        sum += weight;
    }

    if (sum <= maxweight)
    {
        std::cout << "Let's go! Total passenger weight: " << sum << '\n';
    }
    else
    {
        std::cout << "This passengers are too heavy by " << sum - maxweight << " pound(s)\n";
    }
}
Don't you know how confusing that is for a beginner? Even as a programmer with decades of experience, I have to stop and figure out what lines 11 and 12 are doing.

Sorry, what? That's just a simple for loop, surely? One of the earliest constructs one learns.

In any case, this is the General forum, not the Beginners forum. There's no expectation here that answers be tailored for a beginner. If someone chooses to post here instead of in Beginners, they're indicating that they want a response on a general C++ programming level, not on a beginners' level.
Give them the real code. If it is confusing, they will ask about it, and you can explain it or throw a link etc. Its best they see some examples of what c++ is becoming, because it seems like most students are learning what it was when I learned it... 2011 and earlier styles/techniques abound.

That said, stuffing the loop activity in the for loop header is nice and compact at the cost of readability. I usually avoid doing that unless the goal is to compact the code.
Last edited on
I think the [ab]use of comma operator is ugly as heck.
Topic archived. No new replies allowed.