Learn C++ code English? + Using void variables in main

Hello

I have two questions and though it'd be unnecessary to make two threads. :)
So, here they come.

Question 1: Learning C++ code English?


I've successfully made a word generator in C++.
(Using for loops, srand, and if/else statements).

But it only output things like:

ajbksvl
znjvef
efe

Really random things who aren't words...

Is it possible to let it follow a pattern/code it/give it examples/whatever matter, but in big lines).

That'd be extremely awesome! :)

Question 2: Using void variables in main?


So, I've expressed variables in a void-function, but I need to use them in my main-function as well. Is it possible to do this?
1
2
3
4
5
6
7
8
9
10
11
12
13
void variables() {
int x;
x = 0;
bool a = false;

string name;
name = "Nielyboyken";
}

int main() {
variables();
//Code to do a lot with variables
}


Thanks for the help! ;)

Thanks for reading,
Niely
1. Research Markov chains.
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.
@Helios:
I understand it a bit yeah. :)
I found this after a bit searching:
http://ben.akrin.com/?p=779

How exactly do I implement it in C++?

@Anon:
Okay thanks, I'll find a solution for it. :)
Possible to let main store variables for the void?
Because the void actual changes the value of a specific boolean which is used in the main-function.
2. Use a struct.
@OP:

2) You can pass your variables into your function by reference:

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
^That seems a good solution. :)
But could you show how it's done with multiple variables?
2. Use a struct.
@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
^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
Topic archived. No new replies allowed.