Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++, variable carried between functions


  • Please log in to reply
6 replies to this topic

#1 bigalexe

bigalexe

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Michigan, USA
  • Local time:04:04 PM

Posted 24 September 2011 - 07:20 PM

I am taking a programming class so I am not looking for anyone to write the code out for me letter for letter, I just need to know where to look. The language to which this question pertains is C++.

I am writing a program that does stuff with list of numbers (common problem). I have decided the best way to go about doing this is with an Array. So I have written successfully a portion of the program for the user to enter the array and verify it is correct, and now am writing the sorting part. I decided that since dealing with Arrays will be one very very common problem in this course it would make sense for me to break the program into functions. The first function as I said has the user enter the array, and terminates upon the user confirming it is correct.

So now I am in another function and need to call up that Array that we defined so I can play with it. How do I call it up?

Again the reason I am using functions is because we do so many things with Arrays it seems like the most logical thing as it will allow me to recycle code.
AMD Phenom II X6 2.8ghz
8GB DDR3 RAM
XFX ATI Radeon HD6850 1 GB DDR5, 26" Widescreen HDMI
500GB + 80GB HDD
Windows 7 Pro, Mozilla Firefox, AutoCAD 2011, Solidworks 2009
1/19/2012

BC AdBot (Login to Remove)

 


#2 Didier Stevens

Didier Stevens

  • BC Advisor
  • 2,734 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:04 PM

Posted 25 September 2011 - 12:32 PM

You need to post your code so we can understand where your problem lies exactly. For example, how does the function that populates the array with user input, provide this array to the rest of the program. A static variable, or returning the array?

Furthermore, you're talking about a C++ class and are trying to solve your problem with functions. Since this is a C++ class, aren't you supposed to use object oriented programming? Or have you seen objects yet?
Because if you know objects, I assume your teacher wants you to solve your problem with objects.

Didier Stevens
http://blog.DidierStevens.com
http://DidierStevensLabs.com

SANS ISC Senior Handler
Microsoft MVP 2011-2016 Consumer Security, Windows Insider MVP 2016-2019
MVP_Horizontal_BlueOnly.png

 

If you send me messages, per Bleeping Computer's Forum policy, I will not engage in a conversation, but try to answer your question in the relevant forum post. If you don't want this, don't send me messages.

 

Stevens' law: "As an online security discussion grows longer, the probability of a reference to BadUSB approaches 1.0"


#3 bigalexe

bigalexe
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Michigan, USA
  • Local time:04:04 PM

Posted 28 September 2011 - 09:18 AM

We haven't seen objects yet. In fact object-oriented programming hasn't even been discussed. This is a class for people with no experience in programming at all (and man does it show). I am a little bit ahead of the curve in this class. I actually forced the program to work by just putting everything into the main function for now but I'd still like to pursue what I consider to be a better solution long-term.

An example of what I want to do using a simple variable is the following...

//START
int main()
VariableMaker:
VariableProcessor:
return 0;

void VariableMaker()
int x;
cout << "Please input x \n";
cin >> x

void VariableProcessor()
int y;
y = x+25;
cout << "Result of program I wrote for an example \n";
cout << y;
//END

I'm not at home right now so I don't have the code in front of me but I remember I just called it by a variable name with a size and all that stuff required to define an array. Then I populated it with user input with a series of "cin" statements (cin >> MyArray[0]).

Is there a consistent way to carry any type of static variable from function to function.
AMD Phenom II X6 2.8ghz
8GB DDR3 RAM
XFX ATI Radeon HD6850 1 GB DDR5, 26" Widescreen HDMI
500GB + 80GB HDD
Windows 7 Pro, Mozilla Firefox, AutoCAD 2011, Solidworks 2009
1/19/2012

#4 Didier Stevens

Didier Stevens

  • BC Advisor
  • 2,734 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:04 PM

Posted 28 September 2011 - 03:15 PM

Well, one way do to this without objects is to declare the array in the main function and pass the array as an argument to the functions.
Have you already seen how to declare and pass arguments?

Didier Stevens
http://blog.DidierStevens.com
http://DidierStevensLabs.com

SANS ISC Senior Handler
Microsoft MVP 2011-2016 Consumer Security, Windows Insider MVP 2016-2019
MVP_Horizontal_BlueOnly.png

 

If you send me messages, per Bleeping Computer's Forum policy, I will not engage in a conversation, but try to answer your question in the relevant forum post. If you don't want this, don't send me messages.

 

Stevens' law: "As an online security discussion grows longer, the probability of a reference to BadUSB approaches 1.0"


#5 bigalexe

bigalexe
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Michigan, USA
  • Local time:04:04 PM

Posted 28 September 2011 - 04:48 PM

Good news... I am home from school, and the chemistry lab is still intact as am I. That means I am here to post the code!

I will explain for a second what is supposed to be going on and the code is included here also. One thing you will notice is that I very liberally use the cout function. This is because I have found very quickly in learning that it helps me know where the program broke if it tells me every step of the way what is going on. Also you will notice a bunch of if/then stuff because I like to make the program ask me if I did the last part correctly. So here is what is supposed to be happening in the eyes of the user...

1.) The program asks you to enter 5 numbers individually, you being a good user do so as requested.
2.) The program then spits out the numbers back at you and asks if this is correct.
3.) You reply with "Y" because you didn't screw up... alternately you can use "N" and it will restart the entry process. Any other entries kill the program.
4.) Once the entry is confirmed as correct the program returns the highest number.

You will also notice I am using the "goto" statement somewhat liberally which I keep reading is a bad idea, but they haven't taught me While & For yet and gave me the homework so I'm trying to do what I can.

What follows here is the NON-Working code broken into void type functions. I'm guessing they shouldn't be void but something else.

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

void ArrayEntry()
{
doOver: //in case you entered the wrong numbers

const int ARRAY_SIZE = 5; //defines array size as a variable.. when changing array size change it on this line!
int MyArray[ARRAY_SIZE]; //defines the array as MyArray at size variable

int xi=ARRAY_SIZE-1; //defines variable xi as maximum array, this is how we know what value we are inputting.

entryPoint: //a goto point for the if/then loop

/*
In theory this is how this works: The first value input above is at maximum slot, then values are input
at each slot decreasing by 1. When the slot number is below 0 (thus making it irrelevant) it should
automatically print out the array.
*/

if (xi>=0)
{
cout << "Input Array Value #" << xi <<" \n";
cin >> MyArray[xi]; //inputting array values now.
xi=xi-1;
goto entryPoint;
}
else
{
cout << "This is the array that you input \n";
cout << MyArray[0] << "\n";
cout << MyArray[1] << "\n";
cout << MyArray[2] << "\n";
cout << MyArray[3] << "\n";
cout << MyArray[4] << "\n";
cout << "Is this correct? Y or N \n";

string YN; //This block asks you to verify that is in fact the array you wanted.
cin >> YN;
if (YN=="Y")
{
//this space intentionally left blank... it jumps out of the loop to position noted in comment "Continue Point"
}
else
if (YN=="N")
goto doOver;
else
cout << "Aborting Operation \n";
} //Finally we have entered an array, verified it is the one we want, and we can work with it!
}

void ArrayProcessing()
{
cout<<"Welcome to the sorting routine \n";

int swap;
int p=0;
swap=MyArray[p];

positionHold:
p=p+1;
if (swap<MyArray[p])
{
swap=MyArray[p];
goto positionHold;
}
else
if (p>ARRAY_SIZE)
{
cout<<swap<<"\n";
cout << "Endless looping bad! \n";
}
else
goto positionHold;
}

int main()
{
ArrayEntry();
ArrayProcessing();
}
AMD Phenom II X6 2.8ghz
8GB DDR3 RAM
XFX ATI Radeon HD6850 1 GB DDR5, 26" Widescreen HDMI
500GB + 80GB HDD
Windows 7 Pro, Mozilla Firefox, AutoCAD 2011, Solidworks 2009
1/19/2012

#6 bigalexe

bigalexe
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Michigan, USA
  • Local time:04:04 PM

Posted 29 September 2011 - 02:00 PM

I think I may have found me confusion point.

The way my brain thinks that things should work is that I "Pull" the variable from one function to another.

IE:
Function#1 executes and makes some variables. Like say a factory produces an item and throws it into a shipping crate.
Function#2 executes and includes instructions to go get variables (produced items), from Function #1 (get them out of the shipping crates).

The more I read it seems to make sense that my brain is backwards to how it actually works. You have to PASS variables.

IE:
Function#1 executes and includes instructions that it needs 3 variables (bill of materials). It passes on the list of what it needs to function #2.
Function#2 executes and makes the bolts, passes them back to function #1.
Everyone carries on.

This whole time I've been trying to make a variable inside a function, save it, and get it later. I think now I realize I can't do that in that particular manner. I have to know beforehand what I need and specifically ask for it.
AMD Phenom II X6 2.8ghz
8GB DDR3 RAM
XFX ATI Radeon HD6850 1 GB DDR5, 26" Widescreen HDMI
500GB + 80GB HDD
Windows 7 Pro, Mozilla Firefox, AutoCAD 2011, Solidworks 2009
1/19/2012

#7 bigalexe

bigalexe
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Michigan, USA
  • Local time:04:04 PM

Posted 29 September 2011 - 03:05 PM

Ok this is the LAST time I repost in my own thread. What follows is the solution to the problem I was having. I declared the Array and it's size outside of the individual functions. Now the sorting routine is completely fraking borked for some reason but I'm assuming that is fixable. Thank you for assisting me in what way you could.

//START CODE
#include<iostream>
#include<string>
using namespace std;

const int ARRAY_SIZE = 5;
int MyArray[ARRAY_SIZE];

void ArrayEntry()
{
doOver: //in case you entered the wrong numbers

int xi=ARRAY_SIZE-1; //defines variable xi as maximum array, this is how we know what value we are inputting.

entryPoint: //a goto point for the if/then loop

/*In theory this is how this works: The first value input above is at maximum slot, then values are input
at each slot decreasing by 1. When the slot number is below 0 (thus making it irrelevant) it should
automatically print out the array.*/

if (xi>=0)
{
cout << "Input Array Value #" << xi <<" \n";
cin >> MyArray[xi]; //inputting array values now.
xi=xi-1;
goto entryPoint;
}
else
{
cout << "This is the array that you input \n";
cout << MyArray[0] << "\n";
cout << MyArray[1] << "\n";
cout << MyArray[2] << "\n";
cout << MyArray[3] << "\n";
cout << MyArray[4] << "\n";
cout << "Is this correct? Y or N \n";

string YN; //This block asks you to verify that is in fact the array you wanted.
cin >> YN;
if (YN=="Y")
{
//this space intentionally left blank... it jumps out of the loop to position noted in comment "CONTINUE POINT"
}
else
if (YN=="N")
goto doOver;
else
cout << "Aborting Operation \n";
//CONTINUE POINT
} //Finally we have entered an array, verified it is the one we want, and we can work with it!
}

void ArrayProcessing()
{
cout<<"Welcome to the sorting routine \n";

int swap;
int p=0;
swap=MyArray[p];

positionHold:
p=p+1;
if (swap<MyArray[p])
{
swap=MyArray[p];
goto positionHold;
}
else
if (p>ARRAY_SIZE)
{
cout<<swap<<"\n";
cout << "Endless looping bad! \n";
}
else
goto positionHold;
}

int main()
{
ArrayEntry();
ArrayProcessing();
}
AMD Phenom II X6 2.8ghz
8GB DDR3 RAM
XFX ATI Radeon HD6850 1 GB DDR5, 26" Widescreen HDMI
500GB + 80GB HDD
Windows 7 Pro, Mozilla Firefox, AutoCAD 2011, Solidworks 2009
1/19/2012




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users