variable is used without being initialized

when I debug this in my MS Visual Studio, I get a message saying variable is used without being initialized, could any1 take a look at what's wrong with my code?

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
#include<conio.h>

using namespace std;

void DisplayHeading();
void GetScore (int);
void DetermineGrade (int,char);
void DisplayGrade (int,char);
void GetResponse(char);

int main()
{
        int Score;
	char Response, Grade;
	
	DisplayHeading();

	do
	{
		GetScore(Score);
	
		DetermineGrade(Score, Grade);
	
		DisplayGrade(Score, Grade);
		
		GetResponse(Response);

	} while (Response == 'y' || Response == 'Y');

	_getch();

	return 0;
}
//----------------------------------------------

void DisplayHeading()
{
	cout << "Grade Calcuator" << endl;
}

//-----------------------------------------------

void GetScore (int score)
{
	cout << "Enter your score between 0 and 100 inclusive: ";
	cin >> score;

	while (score > 100 || score < 0)
	{
		cout << "Invalid Score! " << endl;
		cout << "Enter a score between 0 and 100 inclusive: ";
		cin >> score;
	}
}

//-------------------------------------------------------

void DetermineGrade (int score, char grade)
{
	if (score >= 85)
	grade == 'A';
	
	else if (score >= 70)
	grade == 'B';
	
	else if (score >= 60)
	grade == 'C';

	else if (score >= 50)
	grade == 'D';

	else
	grade == 'F';
}

//---------------------------------------------

void DisplayGrade(int score, char grade)
{
	cout << "The score is " << score << endl;
	cout << "The Grade is " << grade << endl;
}

//------------------------------------------------
void GetResponse (char response)
{
	cout << "Do you want to calculate another grade? (Y/N)";
	cin >> response;

	do
	{
		cout << "Invalid response!" << endl;
		cout << "Please enter a valid response: ";
		cin >> response;
	} while (response != 'Y' || response != 'y' || response != 'N' || response != 'n');
}
The reason is the value that you input from the functiion is not returning back to main
Use reference or pointer parameter in getscore and determinegrade
Thank you very much, this problem has been solved... but another problem occurred...

my program keeps outputting

"Invalid response!"
"Please enter a valid response:

no matter what letter I enter, and the program won't go back to the beginning after calculate one grade at the first time
Try to use && instead of ||
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
#include<conio.h>

using namespace std;

void DisplayHeading()
{
	cout << "Grade Calcuator" << endl;
}

//-----------------------------------------------

int GetScore()
{
	int score;

	cout << "Enter your score between 0 and 100 inclusive: ";
	cin >> score;

	while (score > 100 || score < 0)
	{
		cout << "Invalid Score! " << endl;
		cout << "Enter a score between 0 and 100 inclusive: ";
		cin >> score;
	}
	return score;
}

//-------------------------------------------------------

char DetermineGrade(int score)
{
	char grade;

	if (score >= 85)
		grade = 'A';

	else if (score >= 70)
		grade = 'B';

	else if (score >= 60)
		grade = 'C';

	else if (score >= 50)
		grade = 'D';
	else
		grade = 'F';

	return grade;
}

//---------------------------------------------

void DisplayGrade(int score, char grade)
{
	cout << "The score is " << score << endl;
	cout << "The Grade is " << grade << endl;
}

//------------------------------------------------
char GetResponse()
{
	char response;

	Beginning:
	cout << "Do you want to calculate another grade? (Y/N) ";
	cin >> response;
	if (response == 'y' || response == 'Y' || response == 'n' || response == 'N')
		return response;
	else
	{
		cout << "Invalid response." << endl;
		goto Beginning;
	}
}


int main()
{
	int Score = 0;
	char Response = 0 , Grade = 0;
	
	DisplayHeading();

	do
	{
		Score = GetScore();
	
		Grade = DetermineGrade(Score);
	
		DisplayGrade(Score, Grade);
		
		Response = GetResponse();

	} while (Response == 'y' || Response == 'Y');

	_getch();

	return 0;
}

Here try this.
Last edited on
OMG!! Thank you very much! It works perfectly :D:D:D
Topic archived. No new replies allowed.