Hello,

I am supposed to write a statement for a Codelab review test that asks:

"Given that two int variables, total and amount, have been declared, write a loop that reads integers into amount and adds all the non-negative values into total. The loop terminates when a value less than 0 is read into amount. Don't forget to initialize total to 0."

I entered in:

total=0;
for (amount=1; amount>0; amount++){
cin >> amount;
total += amount;
}

When I submit my response, I am told that I am not correctly calculating the correct value.

As far as I see it, the loop continues until a negative number is entered, so cin keeps getting values, and total is accumulating the values. Is my logic wrong?

Thanks for your help, I do not have a full script because we just enter snippets.

I think the main logic is correct, but I did spot one tiny mistake (I think):

While it is correct that the loop will exit when a negative number is entered, the negative number is still added to total (Note that amount is added to total before the check …

ok let's imagine the user enters 4 then -6, the total *should* be 4, but here is what really happens:

1- total initialized to 0
2- for loop starts (amount initialized to 1)
3- looping condition (amount >= 0) returns true (looping continues)
4- cin reads 4 from user …

## All 5 Replies

I think the main logic is correct, but I did spot one tiny mistake (I think):

While it is correct that the loop will exit when a negative number is entered, the negative number is still added to total (Note that amount is added to total before the check of the for loop).

I hope I've helped :)

Khaled Zaidan

Note 1: Although it doesn't affect the functionality of the code, "for (amount=1; amount>0; )" would be more accurate than "for (amount=1; amount>0; amount++)", there is no need to increment amount

Note 2: "when a value less than 0", this means you need to change the condition to "amount>=0" instead of "amount>0"

Thank you Khaled,

I made the changes you suggested about the >=, but still same error!

I didn't really understand what you advised:
(Note that amount is added to total before the check of the for loop).

Could you explain this to me. I am just starting out and I seem to be confused when doing running totals. Is there a different way I should be doing it?

Thanks again,
Natalie

ok let's imagine the user enters 4 then -6, the total *should* be 4, but here is what really happens:

1- total initialized to 0
2- for loop starts (amount initialized to 1)
3- looping condition (amount >= 0) returns true (looping continues)
4- cin reads 4 from user input (now amount = 4)
5- 4 is added to total (now total is set to 4)
6- looping condition (amount >= 0) return true (looping continues)
7- cin reads -6 from user input (now amount = -6)
8- -6 is added to total (now total is set to -2)
9- looping condition (amount >= 0) returns false (loop terminates)

result: total = -2 (not correct)

So I was talking about steps 8 and 9, amount is being added before it was being checked for being non-negative.

A simple fix would be:

if (amount >= 0) {
total += amount;
}

Tell me how it goes :)

Khaled Zaidan

btw, a much cleaner solution would be using a while loop:

``````total=0;
cin >> amount;
while (amount >= 0) {
total += amount;
cin >> amount;
}``````

Khaled Zaidan

Hi again,

That was an awesome explanation! You are right about the while loop, it is much better. I often just start to immediately write a for loop and I try to keep all the action in that one loop. I am slowly starting to grab the logic and teacher is of no help. You would make a great tutor, do you live in NYC and in need of some extra \$\$ cash? I have been desperately trying to find a tutor that can break it down and not confuse me more but have had no success. They are either too techie or soooo advanced they can't understand my roadblocks!At least I always have this forum for help!My next exercise is the dreaded STRINGS section, lol.

Thank you for taking your time to help me, I really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.