C++ error during VS2015 debugging-function not returning value

Hello fellow C++ coders. Below is an error message I received when I debugged by C++ code using VS2015 on Windows 8

Run-Time Check Failure #3 - The variable 'vfracmelan' is being used without being initialized.

The program '[12872] MonteCarloDemo.exe' has exited with code 0 (0x0).

Here is the function that threw the runtime error saying vfracmelan was not initialised..could someone tell me where I am going wrong:

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
double runMC::getmelanfrac(void) { //units decimal or %
	std::string skinToneChoice;
	double vfracmelan;
	std::cout << "\nChoose a skin tone-type LIGHT, MODERATE OR DARK as a choice: (upper case only)";
	getline(std::cin, skinToneChoice);
	//these numbers refer to the percentage of melanin in epidermis
	if (skinToneChoice == "LIGHT") {
		vfracmelan = printRandomDouble(1.3, 6.401);
		std::cout << "Light skin tone was chosen." << endl;
	}//if

	else if (skinToneChoice == "MODERATE") {
		vfracmelan = printRandomDouble(11, 16.01);
		std::cout << "Moderate/brown skin tone was chosen." << endl;

	}

	else if (skinToneChoice == "DARK") {
		vfracmelan = printRandomDouble(18, 43.01);
		std::cout << "Dark skin tone was chosen." << endl;

	}

	else {
      std::cout << "String input for skin pigmentation(tone) could not be recognised-restarting function. " << endl;
		getmelanfrac();
	}
	return vfracmelan;

}//function 



Note: printRandomDouble is from a header file I included #include "rand_num.h". See below:

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
40
41
42
43
44
45
46
47
48
#ifndef RAND_NUM_H
#define RAND_NUM_H

#include <iostream>
#include <random>
#include <chrono>
#include <time.h>
#include <stdio.h>
int printRandomInt(int min, int max) {

	unsigned long seed = (unsigned long) std::chrono::steady_clock::now().time_since_epoch().count();
	std::default_random_engine e1(seed);
	std::uniform_int_distribution<int> distrA(min, max); //this is an inclusive range [min, max]
	return distrA(e1); //This is fine to use for he project
}


double printRandomDouble(double min, double max) {


	unsigned long seed = (unsigned long) std::chrono::steady_clock::now().time_since_epoch().count();
	std::default_random_engine e2(seed);
	std::uniform_real_distribution<double> distrB(min, max); //this is an inclusive-not inclusive range [min, max)
	return distrB(e2); //This is fine to use for he project


}

float printRandomFloat(float min, float max) {

	unsigned long seed = (unsigned long) std::chrono::steady_clock::now().time_since_epoch().count();
	std::default_random_engine e3(seed);
	std::uniform_real_distribution<float> distrC(min, max); //this is an inclusive-not inclusive range [min, max)
	return distrC(e3); //This is fine to use for he project

}
double NormalDistributedVals(double x2, double mu_1, double mu_2, double sigma_1, double sigma_2, double rho) {
	unsigned long seed = (unsigned long) std::chrono::steady_clock::now().time_since_epoch().count();
	std::default_random_engine e4(seed);
	double output;
	std::normal_distribution<double> distrN(mu_1 + (sigma_1 / sigma_2)*rho*(x2 - mu_2), sqrt(1 - pow(rho, 2)* pow(sigma_1, 2)));

	output = distrN(e4);

	return output;
}

#endif /*RAND_NUM_H*/ 


Could someone please tell me what the issue as I feel it could be something I am not seeing that you may see. Thank you.


There is a path through your runMC::getmelanfrac() method that results in vfracmelan never having a value assigned to it, but still being returned.

(Also, there's really no need to use recursion here. A simple loop will suffice, and would be much clearer.)
@MikeyBoy What path through runMC::getmelanfrac(), could you be more specific; and yes a do-while loop would be better I admit.
Well, I was hoping you'd look through it and see it yourself, but apparently not...

Look at what happens if your function drops through to the "else" block. vfracmelan isn't being set anywhere there.
Topic archived. No new replies allowed.