Aaron Landis
Aaron Landis

Reputation: 21

Simple program, cannot get for loop to execute

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!

Upvotes: 0

Views: 75

Answers (4)

David
David

Reputation: 10708

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.

Upvotes: 0

Vicente Cunha
Vicente Cunha

Reputation: 205

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.

Upvotes: 1

Van Tr
Van Tr

Reputation: 6103

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.

Upvotes: 2

tkausl
tkausl

Reputation: 14269

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.

Upvotes: -1

Related Questions