#include<iostream.h>
#include<conio.h>
int main(void)
{
    void hello()
    {
        cout<<"HI";
    }
    getch();
    return 0;
}

I am getting an error at line 6 stating that declarartion syntax error. What do I do?

1) Get an up to date compiler (many are free like this one:

http://sourceforge.net/projects/orwelldevcpp/

2) Do not use conio.h ... or getche ... to keep code portable

3) Define your functions outside of main function ... before called

4) See example of how modern C++ looks ...

// hello.cpp //

#include <iostream>


void hello()
{
    std::cout<<"HI";
}


int main(void)
{
    hello();

    // keep 'window open' until 'Enter' key is pressed 
    std::cout << "\n\nPress 'Enter to continue/exit ... " << std::flush;
    std::cin.get();

    return 0;
}

Edited 1 Year Ago by David W

You are not allowed to define a function in a function and that is what the compiler is complaining about. There are two solutions to this. You could move the function out of main and put it in the global scope or you can change the function into a lambda which is allowed at the local scope.

#include<iostream>
// global function decleration
void hello()
{
    std::cout << "HI";
}

int main()
{ 
    std::cin.get(); // used to pause program.  press enter to continue
    return 0;
}

or

int main()
{ 
    auto func = []() { std::cout << "HI"; };
    func();
    std::cin.get(); // used to pause program.  press enter to continue
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.