Click here to Skip to main content
15,348,965 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <bits stdc++.h>
using namespace std;

int main() {
    int sum=0;
    int n;

    while(1){
        cin >> n;
        sum += n;
        if(sum<0)
            break;
        cout << n << '\n';
    }

    return 0;
}


What I have tried:

sum = 0
while sum > 0:
    num = int(input())
    sum = sum + num
    if sum >= 0:
        print(num)
    else:
        break
Posted
Updated 20-Mar-22 19:05pm
v2
Comments
jeron1 9-Sep-20 14:00pm
   
I don't know anything about python,
but the first 2 lines seem to be incompatible (the while loop will never be executed as 'sum' is initially not greater than zero). But there's a good possibility that I am completely wrong.
Patrice T 9-Sep-20 15:42pm
   
And you have a question ?

Quote:
sum = 0
while sum > 0:
num = int(input())
sum = sum + num
if sum >= 0:
print(num)
else:
break

Your code first assigns 0 to sum and then checks if sum itsef is greater than 0.
It is very easy to mimic the C++ code beahviour:
Python
sum = 0
while True:
    num = int(input())
    sum = sum + num
    if sum < 0:
        break
    print(num)
   
Comments
Patrice T 8-Jan-22 2:06am
   
+5
CPallini 8-Jan-22 3:30am
   
Thank you, :-D
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be “good code” in the target language – they are based on very different frameworks, and what makes something work in one language does not always “translate” directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work “straight out of the box”.
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it w=from scratch using the original as a “template”. You will get a much, much better result that will save you a lot of time in the long run.
   
Python
sum = 0
while sum > 0:
    num = int(input())
    sum = sum + num
    if sum >= 0:
        print(num)
    else:
        break

You have problems with resulting code of your translation, now, it is time to learn tools and techniques that will help you to understand where and how your code fail. The tool is debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
v3
Comments
Stefan_Lang 8-Jan-22 9:43am
   
This is a question from 2020
Patrice T 8-Jan-22 12:30pm
   
Thanks for spotting it, I didn't saw it. Light gray again.
Do you think it is the reason of downvote ? or it advising about debugger ?
Downvoter was too shy to give a reason.
Stefan_Lang 8-Jan-22 14:11pm
   
No idea who downvoted. I hate when they do that without giving a reason. Expect me to do so if I ever downvote you or anyone else ;-)

As for the topic being listed at the top, I guess that either someone posted something that was deleted afterwards, or something else caused it's date to be modified: the OP states it's just been updated, but the last update was in 2020 too...
Patrice T 8-Jan-22 14:20pm
   
I know for sure that there is buggs in CP handling of date and updates.
I have seen questions popping on top of list with a supposedly update from me, which I didn't.
CHill60 10-Jan-22 7:36am
   
If someone posts a new solution then the date update will be updated. Frequently that new "solution" is spam caught in the moderation filter and so never appears in the Question's forum but the question is still "reactivated" with today's date as the last updated date. It can then appear that V2 was created "today" but if you click on the revisions the correct date from the last edit / version does appear
Patrice T 10-Jan-22 8:12am
   
I have seen questions popping on top of list with no apparent change, but an improvement of question with my name as the one how did the improvement.
And I know for sure that I did nothing recently on this question.
CHill60 10-Jan-22 9:50am
   
Might be a caching thing - it says Rick York now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900