Not sure what I am doing wrong.

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
// The function displayMessage is repeadedly called from a loop.
#include <iostream>
using namespace std;
 
 //************************************************************
 //Definition of functional displayMessage                    *
 //This function displays a greeting                          *
 //************************************************************
 
 void displayMessage()
 
{
    cout << "Hello from the function displayMessage.\n";
}

//*************************************************************
// Function main                                              *
//*************************************************************

int main()
{
    cout << "Hello from main.\n";
    for ( int count = 0; count < 5; count++ )
        displayMessage(); //Call displayMessage
    cout << "Back in function main again.\n";
    system ("PAUSE");
    return EXIT_SUCCESS;
}


I am suppose to get an output of:

Hello from main.
Hello from the function displayMessage.
Hello from the function displayMessage.
Hello from the function displayMessage.
Hello from the function displayMessage.
Hello from the function displayMessage.
Back to function main again.

I believe the problem to be in my for loop but I don't spot it. In stead I get

Hello from main.
Hello from the function displayMessage.
Back in function main again.
Press any key to continue . . .

can anyone check me over because I just don't see it.
Last edited on
This won't even compile, as you have an apostrophe instead of a semicolon at the end of line 25.
Fixing that, the program will give the expected output.
still get the same thing ment to put the ; at the end on line 25.
I was able to compile and run your program, with no problems. I changed system ("PAUSED"); to system ("PAUSE"); as I received an error, but it still compiled. ( There is NO system command, "PAUSED" ) If your using Microsoft Visual C++, then you may need to add #include "stdafx.h" . If you're not using that one, which compiler are you using? Knowing that, someone may know better why your program isn't working, while another one, does.
just compiled and i get
1
2
3
4
5
6
7
Hello from main.
Hello from the function displayMessage.
Hello from the function displayMessage.
Hello from the function displayMessage.
Hello from the function displayMessage.
Hello from the function displayMessage.
Back to function main again.
Are you sure you display the program as you intend to compile it?

Since you only get one call on displayMessage() I guess that the loop is applied to some command having no visual output like:
1
2
    for ( int count = 0; count < 5; count++ );
        displayMessage(); //Call displayMessage 


Only there I can find a possible explanation.
It was the system("paused") not being system("pause") it compiles fine now.
Topic archived. No new replies allowed.