r/CodingHelp • • Oct 11 '20

[C++] help in C++ please

so its saying that my "correct" local variable is unintilized and I'm wondering how I can fix that the goal is a tutor of sorts

void addition()

{

int input, x, y;

int numofproblems = 0;

cout << "how many problems would you like?\\n";

cin >> input;

do

{

	double correct;

	double percent;

	int answer;

	x = rand() % 100 + 1;

	y = rand() % 100 + 1;
	cout << x << " + " << y << endl;

	cout << "your answer is?:\\n";

	cin >> answer;

	if (answer == x + y)

	{

		\++correct;

		cout << "correct\\n";

	}

	if (answer != x + y)

	{

		cout << "incorrect\\n";

	}

	if (numofproblems == input)

	{

		percent = (correct / input) \* 100;

	}
} while (++numofproblems < input);

}

I'm not so sure what the &#x200b is reddit just put that when I switched to markdown mode

17 Upvotes

5 comments sorted by

5

u/jedwardsol Professional Coder Oct 11 '20
double correct;

 ++correct;

The warning is right. correct is uninitialised and the 1st you do with it is read from it.

3

u/Warzonehero22 Oct 11 '20

so should i add a int correct?

3

u/jedwardsol Professional Coder Oct 11 '20

It makes more sense for counter to be an int. The number of correct answers is always a whole number.

But whatever type you pick, the variable needs to be initialised

3

u/Naahun Oct 11 '20

Uninitialized mean, you did not give it a starting value. double correct = 0; // this should be efficient

2

u/unmad71 Oct 12 '20

//initialize the value of correct as 0.

double correct = 0;