0

Brand new programmer, so green behind the ears that I could be a frog. The intent of this program is to use a for loop (not a simple formula) to add all integers up to a user designated value. It executes all code up to the for loop perfectly, however the for loop does not seem to run. Here is the code in question:

#include "stdafx.h"
#include <iostream>
int main(int num=0)
{
    int sum = 0;
    std::cout << "This program will sum all integers up to a given value";
    std::cout << std::endl << std::endl;
    std::cout << "Please enter a number "<<std::endl;
    std::cin >> num;
    std::cout << std::endl << "You've chosen the number  " << num<<std::endl;
    for (int i = 1; i <= num; ++i)
        sum += i;
        return (sum);
    std::cout << "The sum of all numbers up to " << num << " is " << sum;
}

Any help is appreciated!

  • 2
    There are several variants, but you should generally use `int main (int argc, char *argv[]) { ... }` for[ "main"](http://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main). You should declare "num" *as a separate variable*!!!! – paulsm4 Jan 28 '16 at 05:41
  • 1
    You are going to get noise for not looking carefully, but its a mistake we all made. Syntax is your enemy. C++ does not look at indentation like Python (#IHATEPYTHONFORTHISREASONONLY), the sum is returned to the operating system that called main(). sum is a variable and doesn't need returning. – ChrisR Jan 28 '16 at 05:41
  • the sum is returned to the operating system that called main() by the second last line before it gets to cout<<. The loop ends at i; on previous line – ChrisR Jan 28 '16 at 05:47
  • Good tips! I clearly have some bad habits to break... – Aaron Landis Jan 28 '16 at 06:00

4 Answers4

2

When it meets return for the first time, the program will be end.

Please put the return at the end of main function.

One more thing, shouldn't use parameter num in main function.

Write a separately function sum_all_integers and then call it in main function.

Van Tr
  • 5,889
  • 2
  • 20
  • 44
1

Good news: your code works! The issue is that when return (sum); is called, the function is exited, and the line that would print out your answer is not executed.

I would also recommend adding the using namespace std; line after your includes, so that you don't end up with so much std:: pollution. Also main should not take any arguments, so put that int num=0 line inside the main block.

Oh, you should also be aware of your return line indentation.

Vicente Cunha
  • 205
  • 1
  • 7
0

In this section

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

Your program will exit.

This boils down to what other answers have mentioned: return from main means exiting the program. Keep in mind that is is true even from within a loop like while or for - doing so is how one might design a search function which exits as soon as it has a result, for example.

To exit a for loop prematurely, use break. To continue to the next iteration and skip further code in the loop, use continue.

Also note that the indentation of return is misleading - it will be executed after the for loop, since the lack of brackets ({}) will make for only loop through the next line, which is sum += i; Keep in mind that C++ is not a whitespace sensitive language, so indentation means nothing to the logic (I like to abuse this in large blocks of similar code by tab-aligning). This is also a good example of why it is common practice to use brackets even when not strictly necessary. EDIT See also Apple's infamous "goto fail" bug.

David
  • 10,458
  • 1
  • 28
  • 40
-1

You return the sum right after the loop:

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

This means, you sum up the numbers and then exit the program before you print the result.

tkausl
  • 13,686
  • 2
  • 33
  • 50