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

Started by
15 comments, last by DejaimeNeto 10 years, 2 months ago

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

Advertisement

You need to either pass in the variable (good), or use a global (bad).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

#include <iostream>
 
using namespace std;
 
 
void function(int x);
 
int main()
{
    int x = 5;
    function(x);
    function(18); //Different example, using value instead of variable.
 
    return 0;
}
 
void function(int x)
{
    cout << x << endl;
}

Hello to all my stalkers.

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

The answer is find a good beginner's C++ tutorial, and start over. You've got a lot to learn, and it will be easier on you (and us) if you take a little time to understand the basics.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


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".

Stay gold, Pony Boy.

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

I used to be called a baby. That was 20+ years ago.

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

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

This topic is closed to new replies.

Advertisement