I guess I am really having trouble with what's inside the function How do I get it out..
Suppose I ask a yes or no question and if the answer is no then break if the answer is yes then compute that formula. okay

int ThingYesNo;
doubleThingHeight
        do
    {
    cout <<"Does the thing exist?"<<endl;//d11
    cout << "1) Yes" <<endl;
    cout << "0) No" <<endl;
    cin>>ThingYesNo;

        if (ThingYesNo==0)
            break;

        else 

        {cout << "What is the Things Height?"<<endl;
        cin>>ThingHeight;
        }

    }while (ThingYesNo !=1);

so later in the program I need to retreive the yes answer to ThingYesNO and I need to retreive ThingHeight As they are both used for future calcuations....How do I write that?

I keep reading and studing but I must have missed something???

Totally Confussed:'(

Recommended Answers

All 17 Replies

int ThingYesNo;
doubleThingHeight
        do
    {
    cout <<"Does the thing exist?"<<endl;//d11
    cout << "1) Yes" <<endl;
    cout << "0) No" <<endl;
    cin>>ThingYesNo;

        if (ThingYesNo==0)
            break;

        else 

        {cout << "What is the Things Height?"<<endl;
        cin>>ThingHeight;
        }

    }while (ThingYesNo !=1);

    // ThingHeight and ThingYesNo are available here. You can use them.

cout << ThingHeight; // I just used it.
cout << ThingYesNo;  // I just used it.
double a = ThingHeight + ThingYesNo; // used in formula.

See lines 21 through 25. I added these. What exactly are you trying to do that isn't working?

If this is part of a c++ class then make those two variables members of the class. If not, then you can declare them in main() and pass them around to the functions that use them. In the case of the function you posted, you will have to pass them by reference to that function so that that function can change their values.

If this is part of a c++ class then make those two variables members of the class. If not, then you can declare them in main() and pass them around to the functions that use them. In the case of the function you posted, you will have to pass them by reference to that function so that that function can change their values.

This makes sense. I wasn't clear where the OP wanted to use these variables later. If it's after the function has returned and the variables were declared inside the function, then they would now be out of scope. If the OP wants to use them inside the function and/or they were passed by reference to the function, that's not an issue.

> this is not homework but rather my work
I'm shocked that someone with a programming job can't solve this trivial problem :-O

> this is not homework but rather my work
I'm shocked that someone with a programming job can't solve this trivial problem :-O

I'm not after reading this thread.

> this is not homework but rather my work
I'm shocked that someone with a programming job can't solve this trivial problem :-O

I would be if I'd not long ago given up all hope that junior programmers have any skills whatsoever any more.
All they seem to be able to do after posting homework questions on forums for years is leach project budgets.

> this is not homework but rather my work
I'm shocked that someone with a programming job can't solve this trivial problem :-O

The Queen of Excel but not a programer. I have a project I have worked on for more than 4 years now working out the math in excel because I am broke and can not afford a programer I decided to learn myself. Thanks in advance for any assistance you can provide

I guess I am really having trouble with what's inside the function How do I get it out..

You get it out by passing it in in the first place. So for example

struct theThing
{
    int ThingYesNo;
    double ThingHeight;

    // zero-initialize the member variables
    theThing() : ThingYesNo(0), ThingHeight(0)
    {}
};

// theThing is passed in by reference, so you can modify it
// and use later on ...
void theThingFunction(theThing & t)
{
        do
    {
    cout <<"Does the thing exist?"<<endl;//d11
    cout << "1) Yes" <<endl;
    cout << "0) No" <<endl;
    cin>> t.ThingYesNo;

        if (t.ThingYesNo==0)
            break;
        else 
        {cout << "What is the Things Height?"<<endl;
        cin>>t.ThingHeight;
        }

    }while (t.ThingYesNo !=1);
}

int main()
{
    theThing t;
    theThingFunction(t);
    // here you can access what's stored in the 't'

    return 0;
}

There are other alternatives for this, like passing a pointer to the function, or allocating (using operator new) a theThing struct inside the function and returning a pointer to it. Anyway, the above should do 'the thing' that you are looking for.

[EDIT]
Expanding a little on how you can do the same without using a struct.
You can also pass by reference/pointer the individual variables into a function,
so that would change the function's signature to

// Arguments passed by reference
void theThingFunction(int & ThingYesNo, double & ThingHeight);

// and use it like ...
int ThingYesNo = 0;
double ThingHeight = 0;
// call the function
theThingFunction(ThingYesNo, ThingHeight);

First Thanks for your kind assistance.

Maybe I posted in the wrong spot I am sorry. I thought this was a place for the amateur to ask questions, which I am. I am trying to learn.

I thought first if I could just get the math to work but it is apparent I need to study more.

Thanks again, I believe this solved my current problem.

TheThingYesNo and ThingHeight are never changed but rather used to calulate other things.

Thanks for your example it is most helpful

Maybe I posted in the wrong spot I am sorry. I thought this was a place for the amateur to ask questions, which I am. I am trying to learn.

I think there's no need for you to be sorry, i.e. while you are learning you can always post questions here.

PS. On a side note, after having read your first post, I was under the impression that you are actually a programmer hired by a company struggling with this problem. I (and probably many others too) was astonished by that, but now I know better.

I think there's no need for you to be sorry, i.e. while you are learning you can always post questions here.

PS. On a side note, after having read your first post, I was under the impression that you are actually a programmer hired by a company struggling with this problem. I (and probably many others too) was astonished by that, but now I know better.

I am sorry that I should have give more back ground.

It is difficult to describe all the facets of my jobs but here's the short of it.

I do math...give me a problem I will solve it in excel
I collect Money owed to my clients
I fight insurance companies
I help small companies re-organize
I am a trouble shooter . My primary function is to dissect the process, discover the problems, and implement a corrective process.

I think that sums it up

I opperate on one main rule

"It's already broke...so what am I going to do broke it some more"

in other words I don't know the limits to my own abilities. If I want to do something then I read, research and learn. I think John Gardner said it best "We are all faced with a series of great opportunities brilliantly disguised as insoluble problems."

First Thanks for your kind assistance.

Maybe I posted in the wrong spot I am sorry. I thought this was a place for the amateur to ask questions, which I am. I am trying to learn.

DaniWeb is a place where people can learn.

I thought first if I could just get the math to work but it is apparent I need to study more.

There isn't much math involved, at least for the problem you posted. It's mostly logic thinking and learning how to make a C or C++ program do what you want it to do. Just like math, you can't do calculus if you have not yet learned how to add, subtract, multiply and divide. You have to start at the beginning, not somewhere in the middle. Buy yourself an Introduction to C++ programming book and start studying it from page 1, doing all the exercises at the end of each chapter. There is a Read Me thread at the top of this forum that has many excellent book suggestions.

Given that you're so good at excel, why aren't you doing all this in excel to being with?

Given that you're so good at excel, why aren't you doing all this in excel to being with?

I have been asked that many time. Excel is simply to easily broken. It is although a great environment to test math and has many fine abilities it is not a good secure place to program. Maybe in 5 more years I will be ready to market this idea I have. It could happen. I would want to protect my hard work. I could probably market it today if there was a true secure way to lockup excel but there is not. I have managed to break it many times.

On the other hand if someone in this forum need help with excel give buz I will be happy to help.

Thank You,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.