hello, how to solve Run-Time Check Failure #3 - The variable 'choice' is being used without being initialized.

I am doing assignment question. When i run the programme, and key in the input, it will come out "Run-Time Check Failure #3 - The variable 'choice' is being used without being initialized." Can anyone else help me solve the problem...

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void number_subject(int&, bool&);
void score_subject(int[], int[], int);
double average_compute(int[], int[], int);
double frac_to_decimal(int, int);
double deviation_compute(int[], int[], int, double);
double percentage(double&, double&);
void output(float, float, float);
void another_set(char&);
int main()
{
bool quit=false, error=false;
int subject;
double average_score, standard_deviation,percent;
char choice;

do
{
number_subject(subject, error);
if (error=true)
continue;
int*numerator=new int [subject];
int*denominator=new int [subject];
score_subject(numerator, denominator, subject);
average_score=average_compute(numerator, denominator, subject);
standard_deviation=deviation_compute(numerator, denominator, subject, average_score);
percent=percentage(average_score, standard_deviation);
output(average_score, standard_deviation, percent);
another_set(choice);
}while(choice=='y');

system("pause");
return 0;
}

void number_subject(int& subject, bool& error)
{
cout<<"Please enter total number of subjects registered by the students:"<<endl;
cin>>subject;

if (subject<=0)
{
cout<<"Please reenter new value!!!"<<endl;
error=true;
}
}

void score_subject(int numerator[], int denominator[], int subject)
{
cout<<"Please enter a series scores (in fraction) of the "<<subject<<" subjects: "<<endl;
for(int x = 0; x<subject; x++)
{
cin>>numerator[x];
cin>>denominator[x];
}
}

double average_compute(int numerator[], int denominator[], int subject)
{
int sum = 0;

for(int x=0;x<subject;x++)
sum += frac_to_decimal(numerator[x],denominator[x]);

return (sum/subject)*100;
}

double deviation_compute(int numerator[], int denominator[], int subject, double average_score)
{
int sum = 0;
double a, b, standard_deviation;

for(int x = 0; x<subject; x++)
{
a=frac_to_decimal(numerator[x], denominator[x]);
a*=100;
a-=average_score;
b=pow(a,2);
sum+=b;
}
standard_deviation = sqrt((1.0/subject)*sum);

return (standard_deviation);
}

double frac_to_decimal(int numerator, int denominator)
{
double decimal;

decimal = double(numerator)/double(denominator);

return(decimal);
}

double percentage(double& average_score, double& standard_deviation)
{
return((standard_deviation/average_score)*100);
}

void output(float average_score, float standard_deviation, float percent)
{
cout<<"The average of input scores is: "<<average_score<<endl;
cout<<"The standard deviation is: "<<standard_deviation<<endl;
cout<<"The deviation is "<<percent<<" % greater than the average!"<<endl;

if(percent>20)
cout<<"Special attention should be given to the students."<<endl;
else
cout<<"No special attention should be given to the students."<<endl;
}

void another_set(char& choice)
{
bool quit=false;

do
{
cout<<"Try again? (y/n):"<<endl;
cin>>choice;

if (choice=='n')
quit=true;
}while(choice!='y' && choice!='n');
}
The run time debug build code is being overly cautious.

Presumably, the error is occurring when you attempt to call another_set at line 32. The first time through the loop, choice has not been initialized and the run time code is detecting this at the time you call another_set. This is seemingly unwarranted since another_set returns a value for choice by reference.

The easiest fix is to assign choice a value on line 18.

My preference would be to change another_set to a bool function and not pass an argument at all.

1
2
3
4
5
6
7
8
9
10
11
12
bool another_set ()
{    char choice;
    while (true)
    {   cout<<"Try again? (y/n):"<<endl;
        cin>>choice;
        if (choice=='n')
            return false; 
        if choice == 'y')
            return true;       
        // neither y nor n, repeat the question
    }
} 


and change the while condition at line 33:
 
    while (another_set());


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Anyway, thanks for your advice and your help. But what i mean is after 'cin' the choice, if it is 'y', it will be go back to the top and ask the user to key in how many subjects and repeat all the things again. If it is 'n', the programme will stop running. This what I means.
http://www.cplusplus.com/forum/general/112111/#msg612042
foo.cpp: In function ‘int main()’:
foo.cpp:23:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
foo.cpp:15:7: warning: unused variable ‘quit’ [-Wunused-variable]


Now look closely at line 23 if (error=true)
that's a tautology. You probably mean if( error )

So the condition always pass, and line 24 executes (continue)
That means that `another_set()' was never called, and `choice' remains uninitialized when checking the condition of the do-while loop in line 33


By the way, you never release the memory pointed by `numerator' or `denominator'
So, what I need to change to let the programme run properly?
ne555 already told you. You need to fix line 23.
After i fixed the line 23, when i key in first score of subject, it will keep running Try again? (y/n) Try again? (y/n)......So, which part i do wrong again...

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void number_subject(int&, bool&);
void score_subject(int[], int[], int);
double average_compute(int[], int[], int);
double frac_to_decimal(int, int);
double deviation_compute(int[], int[], int, double);
double percentage(double&, double&);
void output(float, float, float);
void another_set(char&);
int main()
{
	bool quit=false, error=false;
	int subject;
	double average_score, standard_deviation,percent;
	char choice;

	do
	{
	number_subject(subject, error);
	if (error==true)
		continue;
	int*numerator=new int [subject];
	int*denominator=new int [subject];
	score_subject(numerator, denominator, subject);
	average_score=average_compute(numerator, denominator, subject);
	standard_deviation=deviation_compute(numerator, denominator, subject, average_score);
	percent=percentage(average_score, standard_deviation);
	output(average_score, standard_deviation, percent);
	another_set(choice);
	}while (choice=='y');

	system("pause");
	return 0;
}

void number_subject(int& subject, bool& error)
{
	cout<<"Please enter total number of subjects registered by the students:"<<endl;
	cin>>subject;

	if (subject<=0)
	{
		cout<<"Please reenter new value!!!"<<endl;
		error=true;
	}
}

void score_subject(int numerator[], int denominator[], int subject)
{
	cout<<"Please enter a series scores (in fraction) of the "<<subject<<" subjects: "<<endl;
	for(int x = 0; x<subject; x++)
		{
			cin>>numerator[x];
			cin>>denominator[x];
		}
}

double average_compute(int numerator[], int denominator[], int subject)
{
	int sum = 0;

	for(int x=0;x<subject;x++)
		sum += frac_to_decimal(numerator[x],denominator[x]);

	return (sum/subject)*100;
}

double deviation_compute(int numerator[], int denominator[], int subject, double average_score)
{
	int sum = 0;
	double a, b, standard_deviation;

	for(int x = 0; x<subject; x++)
	{
		a=frac_to_decimal(numerator[x], denominator[x]);
		a*=100;
		a-=average_score;
		b=pow(a,2);
		sum+=b;
	}
	standard_deviation = sqrt((1.0/subject)*sum);

	return (standard_deviation);
}

double frac_to_decimal(int numerator, int denominator)
{
	double decimal;

	decimal = double(numerator)/double(denominator);

	return(decimal);
}

double percentage(double& average_score, double& standard_deviation)
{
	return((standard_deviation/average_score)*100);
}

void output(float average_score, float standard_deviation, float percent)
{
	cout<<"The average of input scores is: "<<average_score<<endl;
	cout<<"The standard deviation is: "<<standard_deviation<<endl;
	cout<<"The deviation is "<<percent<<" % greater than the average!"<<endl;

	if(percent>20)
		cout<<"Special attention should be given to the students."<<endl;
	else
		cout<<"No special attention should be given to the students."<<endl;
}

void another_set(char& choice)
{
	bool quit=false;

	do
	{
	cout<<"Try again? (y/n):"<<endl;
	cin>>choice;

	if (choice=='n')
	quit=true;
	}while(choice!='y' && choice!='n');
}
> it will keep running Try again? (y/n) Try again? (y/n)
Provide the input that you've used.


Your program still have the same issue.
If you trigger the error in `number_subject()' then you would try to continue the loop, with `choice' uninitialized.
the first input is 3
the next input are 80/100,70/100,90/100

can just show me the answer, bcs tmr i need to pass up ady...
> 80/100
56
57
			cin>>numerator[x];
			cin>>denominator[x];
you are not handling the `/' character

You could do
1
2
char foo;
cin>>numerator[x]>>foo>>denominator[x];
Topic archived. No new replies allowed.