-3
#include <cstdlib>
#include <iostream>

    int GenerateID()
    {
        using namespace std;
        static int nNextID = 0;
        nNextID++;
        if (nNextID <= 20)
        cout << nNextID << endl;
    }

int main()
{
    int GenerateID();
    system("pause");
}

why isn't the above program running? I want it to generate on the console numbers from 1-20 with this program.

In fact, the program is running. However, the program is not generating any output. I expect the output to be the numbers 1 to 20 on one line separated by a space. I observe blank, i.e. no output.

The program does not crash. There are no compiler errors or warnings. In fact, here is a walk-through of the program:

I import two libraries, cstdlib for executing system commands using system and iostream for the input and output objects cin and cout, respectively. They're located in the std namespace so I type using namespace std because I'm lazy and don't want to type std::cout.

Then I initialize nNextID to 0, increment it, and if it's less than 20 it shall output nNextID. This is the end of the function.

I call that function from main and use pause to end the program. So, why do I get blank output when I am expecting 1 to 20 on a line, numbers delimited by spaces?

Jossie Calderon
  • 1,393
  • 12
  • 21

5 Answers5

6

Your main function should be this:

int main()
{
    GenerateID();
    system("pause");
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
3

In main, you didn't call the function. You merely declared it.

Do this instead :

GenerateID();

(ie. without the int).

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
2
int GenerateID();

is not a function call. This is a function declaration that defines the return type and the parameters of the function. This will not call the GenerateID function, it will only allow everything under that definition to use the function properly (which is quite redundant here as you're already declaring and defining the function above int main in the first place.)

To fix this, you should do:

GenerateID();

That would be a proper function call.


I'd also like to warn you about the use of the system function. For fast and simple testing purposes, usage is totally fine, although questionable, but since this function is very Operating System Dependent, I would recommend living without it.

An alternative to system("pause") would be:

std::cin.get();

It will not display the text, but you really don't need that either way. (Thank you David)

I hope this helps!

user123
  • 8,970
  • 2
  • 31
  • 52
  • 1
    Or you can simply use `std::cin.get()` instead. – David G Mar 07 '13 at 16:24
  • Why do you say that `using namespace std;` doesn't belong inside the function's body, but rather at the global scope? Sounds like a terrible recommendation to me. Limiting the scope of that statement should be encouraged. – Benjamin Lindley Mar 07 '13 at 16:33
1

int GenerateID() in main declares a new function, it doesn't call the function you wrote earlier.

In order you call your function, simply use GenerateID();. By the way, your function does not return anything, so there is little point in its return type being declared int - use void.

us2012
  • 16,083
  • 3
  • 46
  • 62
1
  • Remove the int keyword in front of the call to GenerateID() in main.

  • wrap that very call with a loop:

    for (int i = 0; i < 20; i++) 
        GenerateID();
    

Optionally:

  • replace your system function call by one of the solutions offered to this question,

  • separate the code to display the id from the one generating it:

    int GenerateID() {
        static int id = 0;
        return id++;
    }
    
    int main(){
        using namespace std;
        for (int i = 0; i < 20; i++)
            cout << GenerateID() << endl;
        // here the code for console pausing
        return 0;
    }
    
  • it is good practice to have your main function return a value at exit (pretty much like any function returning something), as in the code above,

  • remove the implicit namespace usages, and make all uses explicit. Personnally, as long as the implicit usage is segregated to very specific parts of the code, I don't see it as a problem, but if your code is getting sufficiently large, you might want to use that feature carefully. In this setup, it's clearly not necessary.

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52