The Coin Toss Code Conundrum

Strange. I am getting an "Uninitialized local variable" on the cointoss(void) section of my code for the "return randomnumber;" line I'll post the code I have so far. I just can't tell what is wrong with it. What am I missing?


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
35
36
37
38
39
  #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int cointoss(void)
{
	int randomnumber;
	randomnumber - 1 + rand() % 2;
	return randomnumber;

}

int main()
{
	int howmanytimes = 0;
	int randomnumber = 0;
	string headtail = "";

	cout << "How many times would you like to toss the coin? ";
	cin >> howmanytimes;

	srand((time(0)));


	for (int i = 1; i <= howmanytimes; i++)
	{
		randomnumber = cointoss();
		if (randomnumber == 1)
			headtail = "head";
		else
			headtail = "tail";

		cout << "headtail" << endl;
	}
	system("pause");
	return (0);
}
well it works for me but the results don't seem to be correct.

How many times would you like to toss the coin? 10
headtail
headtail
headtail
headtail
headtail
headtail
headtail
headtail
headtail
headtail
Could it be because of Microsoft visual studio? That's the program I use for coding. I am clearly getting a syntax error for line 11. What program are you using SamuelAdams?
> What am I missing?

In int cointoss(void), the variable randomnumber is not initialised. It is also never assigned to. The function, finally returns the uninitialised value.
1
2
3
4
5
6
7
8
9
int cointoss(void)
{
	int randomnumber; // *** not initialised 

	randomnumber - 1 + rand() % 2; // *** computes a value and then discards the result

	return randomnumber; // *** return the uninitialised value

}


This is probably what you intended to do:
1
2
3
4
5
6
7
8
9
int cointoss(void)
{
	int randomnumber; 

	randomnumber = 1 + rand() % 2; // *** note: = (assignment)

	return randomnumber; // fine: return the value after it was assigned to

}
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
#include <iostream>
#include <cstdlib>
#include <ctime>

// note that this is not a great idea: it relies on the randomness
// of the low-order bit of the pseudo random number generated by
// a random number generator of suspect quality.
// for better options, see: http://en.cppreference.com/w/cpp/numeric/random
bool got_a_head() { return std::rand()%2 ; }

void toss_a_coin()
{
    if( got_a_head() ) std::cout << "head\n";
    else std::cout << "tail\n";
}

int main()
{
    int howmanytimes = 0;
    std::cout << "How many times would you like to toss the coin? ";
    std::cin >> howmanytimes;

    std::srand( std::time(nullptr) ) ;

    for( int i = 0; i < howmanytimes; ++i ) toss_a_coin() ;
}
Alright, SamuelAdams,

Now that I have scrubbed that "-" from line 11 (Thank you, JLBorges!) I can see what you are talking about, SamuelAdams. I am also getting nothing but "headtail" outputs. (Thank you very much JLBorges!) I bow to my betters. Thank you for the help! You guys both helped me learn a lot from this!

Respectfully,
Armygun087

(Marking as solved)
@Armygun087
I am also getting nothing but "headtail" outputs.


As you can see, YOU are telling the program to display "headtail" on line 35. What I think you intended the line to read, was..

cout << headtail << endl;, WITHOUT the quotes.
Topic archived. No new replies allowed.