-4
int sumTo(int value)
{
int total(0);
for (int count=1; count <= value; ++count)
    total += count;

return total;
}

Where do I add cout<<total; to print the results? I have tried to add the print function several locations without success -- either getting nothing or a large number (maybe a number previously stored in the location). Have debugger with the result showing total as 15 when value is equal to 5.

In addition, I have added a void function to print, but still no correct results.

Need help with the proper placement of the print function to yield the correct answer?

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • 3
    It's not clear where the problem lies. Do you know how to call a function? Show your attempt. – François Andrieux Mar 14 '18 at 14:01
  • 2
    Sounds like you could benefit from one of these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ can't be learned by guessing. – Ron Mar 14 '18 at 14:04
  • 3
    [This seems to be a repost](https://stackoverflow.com/questions/49120292). Is this version better? – Drew Dormann Mar 14 '18 at 14:09

1 Answers1

3

This should work :

#include <iostream>

int sumTo(int value)
{
    int total = 0;
    for (int count=1; count <= value; ++count)
        total += count;

    return total;
}

int main(void)
{
    std::cout << sumTo(12);
}
ThomasRift
  • 98
  • 6