开发者

Adding consecutive integers from an input (Translated from Python to C++)

I'd like to request some help on my HW. I think I'm really close to figuring this out. Our CompSci class is currently shifting from learning Python to (introductory) C++. Since the two are vaguely similar, we've been advised, since we're beginners, to code the problem in Python (which we're very familiar with) and to translate it into C++ using the basics we just learned. The problem to solve is a simple "add the consecutive integers from 1 to that number, given a positive integer input." So an example would be:

>>Enter a positive integer: 10
>>1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

The Python code (this was successful) that I'm attempting to translate into C++ is:

num = int(raw_input("Enter a positive integer: "))
sum = 0
for i in range(1, num):
    sum += i
    print i, "+",
print num, "=", sum+num

And my unsuccessful C++ code:

#include <iostream>
using namespace std;

int main()
{
    int num;
    int sum;
    int i;
    sum = 0;
    cout << "Please enter a positive integer: " << endl;
    cin >> num;
    for (i=0; 1 <= num; i++)
        {
        sum = sum + i;
        cout << i << "+" << endl;
        }
    cout << num << "=" << sum + num << endl; 
    return 0;
}

But the output is simply an infinite, non-ending addition sequence from 0 to infinity, going top to bottom. Even worse is that it did not print in a straight line like I want it. As you can see, I quite literally tried to translate it word-for-word; I thought that'd be 开发者_StackOverflow社区foolproof. Something must be wrong with my for loop. Since C++ doesn't have a class of its own for "range" like Python does, I thought the middle condition statement ("1 <= num;") would act as the range. Why didn't my "=" sign print out? And I don't understand why it won't terminate when it reaches "num." Think you can help? I thank you in advance for the replies.


Fixed code:

#include <iostream>
using namespace std;

int main()
{
    int num;
    int sum;
    int i;
    sum = 0;
    cout << "Please enter a positive integer: " << endl;
    cin >> num;
    // Here you had 1 <= num which was always true for positive num
    // and it did not depend on value of i.
    for (i = 1; i < num; ++i)
        {
        sum = sum + i;
        cout << i << "+";  // Here you had endl which produced newline characters.
        }
    cout << num << "=" << sum + num << endl;-
    return 0;
}


This:

for (i=0; 1 <= num; i++)

should be:

for (i=0; i <= num; i++)


try this.

#include <iostream>
using namespace std;

int main()
{
    int num;
    int sum;
    int i;
    sum = 0;
    cout << "Please enter a positive integer: ";
    cin >> num;
    for (i=0; i < num; i++)
        {
        sum = sum + i;
        cout << i << " + ";
        }
    cout <<num << " = " << sum+num << endl; 
    return 0;
}


I don't really know Python, but the code

for i in range(1, num): 

looks really similar to

for (int i=1; i <= num; ++i) 

or is it possibly

for (int i=1; i != num; ++i) 

which looks more like C++?


loop in c++ are most basic than python, the for loop is more simpler, it is based on the three expression: initializer expression, the loop test expression, and the counting expression. In particular what is wrong in your code is the test expression. Remember that the loop is executed if the test expression is true. You need to loop if the condition i<num is true. Your loop is never ending because num is always >= 1, or as you wrote 1 <= num always.

To print everythig on a line don't use endl

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜