Sign in to follow this  
Cryusaki

C++ Functions run when I don't want them to

Recommended Posts

Cryusaki    511

I want to make a function in main that will not run however just making the function causes it to run

I tried prototyping the function but I am using object that are made and in main and that is out of scope
That goes the same for above main, as the variables/objects inside were defined in main
How do I stop the program from running the function without calling it when I create it?

Share this post


Link to post
Share on other sites
Cryusaki    511

My program has SFML so I am not going to post that code but here is simple version of what I mean

#include <iostream>

using namespace std;

int main()
{
    void function();
    {
        cout << "Hello world!" << endl;
    }

    return 0;
}

The program will say Hello World

but I want it to only run when i call the function later on

How do I stop that?

Share this post


Link to post
Share on other sites
DejaimeNeto    4221

My program has SFML so I am not going to post that code but here is simple version of what I mean

#include <iostream>

using namespace std;

int main()
{
    void function();
    {
        cout << "Hello world!" << endl;
    }

    return 0;
}

The program will say Hello World
but I want it to only run when i call the function later on
How do I stop that?

 

I see the problem, as previously noted by Paradigm Shifter and Washu, you're not coding a "function inside main".
What you are doing is declaring a function:
 

void function(); // Notice that ";", it marks the end of the function declaration

 
 
and then adding "Hello World!" to cout.

{
    cout << "Hello World!" << endl;
}

the { and } are just block separators, that you can use to limit scopes, between some other things.
So the function and the { cout } aren't related by any means.

Edited by dejaime

Share this post


Link to post
Share on other sites
Cryusaki    511

I see my issue now, I haven't coded in a long time but I can't believe I made such a basic mistake

Thanks everyone but I have a new issue related to my original issue

How do I make a function that can work with a variable declared in the main?

#include <iostream>

using namespace std;

void function();

int main()
{
    int x = 5;

    return 0;
}

void function()
{
        cout << x << endl;
}

I get an error at second last line saying x is out of scope, I understand why I just don't know how to solve this

Share this post


Link to post
Share on other sites
minibutmany    1998


I try not to use classes in C++ because of how functions are called by themself. C style object orientation is more flexible using structs, virtual function tables and separate methods.

 

Before it was called C++, it was called "C with Classes".

Share this post


Link to post
Share on other sites
DejaimeNeto    4221

Wow. It was a pretty ignorant comment, but -21 is pretty harsh for the beginner's forum. Let's calm down a little, lol.

Actually, it is not only ignorant, but a quite misleading comment, and targeted at an absolute beginner, what makes matters worse. Given that the up vote hover message is: This post provides useful information and demonstrates knowledge of the subject, voting it up would make absolutely no sense.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this