Separate Program into Functions

I'd like to separate my program into different functions.
The code literally explains itself inside the function fDisplayExplanation().
Here is the code with the debriefing at the bottom.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

void fDisplayExplanation(void)
{
    cout << "This program asks the user to enter a loop count,\n"
         << "and then reads the loop count to the user.\n"
         << "The loop count is summed and read to the user.\n"
         << endl;
}

int main(int nNumberofArgs, char* pszArgs[])
{
    fDisplayExplanation();

    //gather desired loop count from user
    int nLoopCount;
    cout << "Please enter loop count: ";
    cin >> nLoopCount;
    cout << endl;

    int nLoopSum = 0;
    for(int i = 1; i <= nLoopCount; i++)
    {
        //add loop count values to nLoopSum
        nLoopSum += i;
        //read loop count to user
        cout << "The loop count is " << i << "."
             << endl;
    }
    cout << endl;

    //read loop count sum to user
    cout << "The total sum of the loop count is " << nLoopSum << ".\n"
         << endl;

    //wait until user is ready before terminating program
    //to allow the user to see the program results
    system("PAUSE");
    return 0;
}


So, you see how there are paragraphs? As in, the nLoopCount section is separated from the for loop section by a space? I'd like each section to be placed into a function (it's not necessary, I know, but for the purpose to better myself with functions). So far, I've failed at every attempt. Feel free to separate the code however you think is more efficient, it doesn't have to abide by the sections I've created. Thanks to all who help me!

P.S. I've read that the best book to read about C++ is The C++ Programming Language by Bjarne Stroustrup, is this true?
I'll do the second one for you, try the others yourself.
1
2
3
4
5
6
7
8
9
10
11
int GetLoopSum(int count){
    int sum = 0;
    for(int i = 1; i <= count; i++)
    {
        sum += i;
        cout << "The loop count is " << i << "."
             << endl;
    }
    cout << endl;
    return sum;
}

and you'd call it with
int nLoopSum = GetLoopSum( nLoopCount );

Note that this function does not need loops (except for the output) and could be
1
2
3
int GetLoopSum(int count){
    return count*(count+1)/2;
}
Thanks alot, so far I've successfully used your function, and then put the nLoopCount section into a working function(renamed nCount). But I'm getting an error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void fDisplayExplanation(void){
    cout << "This program asks the user to enter a loop count,\n"
         << "and then reads the loop count to the user.\n"
         << "The loop count is summed and read to the user.\n"
         << endl;
}

int fGetCount(){
    int nCount;
    cout << "Please enter loop count: ";
    cin >> nCount;
    cout << endl;
    return nCount;
}

int fGetSum(int nCount){
    int nSum = 0;
    for(int i = 1; i <= nCount; i++)
    {
        nSum += i;
        cout << "The loop count is " << i << "."
             << endl;
    }
    cout << endl;
    return nSum;
}

int main(int nNumberofArgs, char* pszArgs[]){
    fDisplayExplanation();
    fGetCount();
    fGetSum(nCount); 

    int nLoopSum = fGetSum(nCount); <----no error here, yet..
    cout << "The total sum of the loop count is " << nLoopSum << ".\n"
         << endl;

    system("PAUSE");
    return 0;
}


On line 36 is says that nCount was not declared in this scope. Makes no sense?
Topic archived. No new replies allowed.