How are these ints uninitialized?

So, bear in mind I am a beginner so I am not to worried about the layout as of right now. Everything up until int matchesWon; works fine. However I get the error that int matchesWon and Draw are uninitialized, even though they are layed out in the same format as the previous ints, which I assigned a value to with cin, how can this be?

[code]
// ConsoleApplication4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int main() {

int scoreteam1;
int scoreteam2;
std::cin >> scoreteam1;
std::cout << "Team one scored " << scoreteam1 << " goals" << std::endl;
std::cin >> scoreteam2;
std::cout << "Team two scored " << scoreteam2 << " goals" << std::endl;

if
(scoreteam1 == scoreteam2)
std::cout << "The match was a draw.";
else if (scoreteam1 > scoreteam2)
std::cout << "Team one won.";
else if (scoreteam1 < scoreteam2)
std::cout << "Team two won.";
std::cin.get();
std::cin.get();

{
int matchesWon;
int matchesDrawn;
int points = (matchesWon * 3) + (matchesDrawn * 1);
std::cout << "How many matches has your team won this season?";
std::cin >> matchesWon;
std::cout << "How many matches has your team drawn this season?";
std::cin >> matchesDrawn;
std::cout << "Your team has " << points << " so far this season";
std::cout << "How many points matches does your team have left to play?" << std::endl;


}

}


[code]
Last edited on
I get the error that int matchesWon and Draw are uninitialized, even though they are layed out in the same format as the previous ints, which I assigned a value to with cin, how can this be?


Do you see that highlighted section? That is the difference, you assigned values to those variables but not to Won and Draw.

But I also use cin for matchesWon and matchesDrew. That is why I'm confused about how they are uninitialized. If anything I assume my use of more { brackets complicates things, cause I'm not really sure how they are meant to be used.
Last edited on
But I also use cin for matchesWon and matchesDrew.

Yes but not until after you have already used those variables in a calculation. Perhaps you need to move that calculation to after you get the information from the user?



Ah I found the problem, it was so obvious lol. int points was calling on the values of the two other ints before they were defined, I get it now.... I just had to define int points after the two cin statements. It now looks like this and works as intended:

[code]

int matchesWon;
int matchesDrew;
std::cout << "How many matches has your team drawn this season?" << std::endl;
std::cin >> matchesDrew;
std::cout << "How many matches has your team won this season?" << std::endl;
std::cin >> matchesWon;
int points = (matchesWon * 3) + (matchesDrew * 1);
[code]
Last edited on
Topic archived. No new replies allowed.