Aug 22, 2014 at 3:19pm UTC
1. Research Markov chains.
Aug 22, 2014 at 3:42pm UTC
Q2. No. x, a and name are local variables within the function variables(). Once variables() has executes, x, a and name go out of scope and are no longer accessible.
Aug 22, 2014 at 5:46pm UTC
^That seems a good solution. :)
But could you show how it's done with multiple variables?
Aug 22, 2014 at 7:27pm UTC
@Duoas
I'm going to guess since he is having trouble with something as straightforward as references that he likely doesn't know what structs are or how to use them.
@Nielyboyken
To show what MikeyBoy is meaning:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <string>
void variables(int &x, bool &a, std::string &name) {
x = 0;
a = false ;
name = "Nielyboyken" ;
}
int main() {
int num = 24;
bool toggle = true ;
std::string myName = "BHX" ;
std::cout << " Before variables(): \n"
<< "num: " << num << " toggle: " << toggle << " myName: " << myName << '\n' ;
variables(num, toggle, myName);
std::cout << " After variables(): \n"
<< "num: " << num << " toggle: " << toggle << " myName: " << myName << '\n' ;
//Code to do a lot with variables
}
Before variables():
num: 24 toggle: 1 myName: BHX
After variables():
num: 0 toggle: 0 myName: Nielyboyken
Struct, using your function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <string>
struct Vars{
int num;
bool truth;
std::string name;
};
void variables(Vars &vars)
{
vars.num = 0;
vars.truth = false ;
vars.name = "Nielyboyken" ;
}
int main() {
Vars samples;
samples.num = 24;
samples.truth = true ;
samples.name = "BHX" ;
std::cout << "Before variables:\n"
<< "samples.num = " << samples.num << "\nsamples.truth = "
<< samples.truth << "\nsamples.name = " << samples.name << "\n\n" ;
variables(samples);
std::cout << "After variables:\n"
<< "samples.num = " << samples.num << "\nsamples.truth = "
<< samples.truth << "\nsamples.name = " << samples.name << '\n' ;
return 0;
}
Before variables:
samples.num = 24
samples.truth = 1
samples.name = BHX
After variables:
samples.num = 0
samples.truth = 0
samples.name = Nielyboyken
Last edited on Aug 22, 2014 at 7:47pm UTC
Aug 22, 2014 at 8:03pm UTC
^Thanks worked! :)
Thanks a lot. ;)
Now searching answer for question 2 !
How to generate English words in C++ (Using Markov Chains)? ;)
(Used struct).
Last edited on Aug 22, 2014 at 8:38pm UTC