Variable being used without being initialized

Jan 27, 2013 at 10:12am
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
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;
int studentdetails_mean();
double standard_deviation();
void barchart();

int studentdetails_mean(){
	ofstream outfile;
	outfile.open("output.txt");
	string student_name;
	int exam_marks[5],sum=0;
	int mean;
	for(int x=0;x<5;x++){
			cout<<"Please enter the student name: ";
			cin>>student_name;
			cout<<"Please enter the examination marks: ";
			cin>>exam_marks[x];
			sum=sum+exam_marks[x];
	}
	mean=sum/5;
	outfile<<"The mean is "<<mean<<endl;
	outfile.close();
	return mean;
}

double standard_deviation(){
	ofstream outfile;
	outfile.open("output.txt");
	int exam_marks[5],students=5,mean;
	double std_dev=0,sox_m,s=0;
	int nminus=4;
	mean;
	for(int ii=0;ii<students;ii++){
		s=s+((exam_marks[ii]-mean)*(exam_marks[ii]-mean));
	}
	std_dev=sqrt(s/nminus);
	cout<<"The result is: "<<std_dev<<endl;
	outfile<<"The standard deviation is: "<<std_dev<<endl;
	outfile.close();
	return std_dev;
}

void barchart(){
	ofstream outfile;
	outfile.open("output.txt");
	int G_A=0,G_B=0,G_C=0,G_D=0,G_F=0,exam_marks[5];
	for(int x=0;x<5;x++){
	if(exam_marks[x]<=39.9){
		cout<<"Grade F"<<setw(15)<<endl;  
		outfile<<"Grade F";
		cout<<"*"<<endl;
		outfile<<"*"<<endl;
		G_F++;
	}else if(exam_marks[x]<=49.9){
		cout<<"Grade D"<<setw(15)<<endl;
		outfile<<"Grade D";
		cout<<"*"<<endl;
		outfile<<"*"<<endl;
		G_D++;
	}else if(exam_marks[x]<=69.9){
		cout<<"Grade C"<<setw(15)<<endl;
		outfile<<"Grade C";
		cout<<"*"<<endl;
		outfile<<"*"<<endl;
		G_C++;
	}else if(exam_marks[x]<=79.9){
		cout<<"Grade B"<<setw(15)<<endl;
		outfile<<"Grade B";
		cout<<"*"<<endl;
		outfile<<"*"<<endl;
		G_B++;
	}else if(exam_marks[x]<=100){
		cout<<"Grade A"<<setw(15)<<endl;
		outfile<<"Grade A";
		cout<<"*"<<endl;
		outfile<<"*"<<endl;
		G_A++;
	}
	outfile.close();
	}
}
int main(){
	int number_stud[5],sum=0;
	double exam_marks[5],mean=0;
	string student_name;
	ifstream infile; // input file
	ofstream outfile; //output file
	infile.open("input.txt"); 
	outfile.open("output.txt");
	
	if(!outfile){  //if cannot open the file
		cout<<"Cannot open file."<<endl;
		return 1;
	}

	string data;

	while(!infile.eof()){
		getline(infile,data);
		cout<<data<<endl;  //output to terminal window
		}
	
	studentdetails_mean();
	standard_deviation();
	barchart();
	infile.close();
	outfile.close();

	system("pause");
	return 0;
}

its showing Run-Time Check Failure #3 - The variable 'mean' is being used without being initialized.
can anyone help me on this ASAP
Jan 27, 2013 at 10:19am
Line 38: You didn't assign "mean" anything before, so it will hold anything what were in memory before (essentyally it's random).
Jan 27, 2013 at 10:20am
What tools are you using? Can you not put breakpoints in your code and debug and see where the problem is located?

BTW at line#36 you have used the local variable named 'mean' without it having been initialised, although it doesn't appear to be doing anything.
Jan 27, 2013 at 11:01am
i am using microsoft visual studio 2012...
Jan 27, 2013 at 11:03am
what value should i use for the mean?
i dont have idea.. help pls
Jan 27, 2013 at 11:09am
what are you trying to accomplish in the first place? What should be "mean" value in your algorythm? I mean, it's you who write it, so you should kmow more than us.
Jan 27, 2013 at 11:14am
well i m creating a program which will show the mean and the standard deviation of the student marks.
this is the question given..

Statistical Calculator

Write a program to read in a sequence of examination mark from a text file named as input.txt and produce the mean, and standard deviation of the marks. Your program should also produce a bar chart for each grade where the range of mark is given as below.

Grade Marks Range
A 80-100
B 70-79.9
C 50-69.9
D 40-49.9
F 0-39.9

Output the result to another text file named as output.txt.
Jan 27, 2013 at 11:14am
i am using microsoft visual studio 2012


Well you should be able to spot issues such as variable x hasn't been initialised. Have you attempted to debug with breakpoints?

How about 5 for the value of mean?
Jan 27, 2013 at 11:28am
i am still new to c++ can you pls tell me how to debug with breakpoints...
Jan 27, 2013 at 11:37am
Try searching the web with such terms as:

"visual studio debug breakpoint"
Jan 27, 2013 at 11:48am
tq very much for the help... now my standard deviation code is not working...
Jan 27, 2013 at 11:52am

tq very much for the help... now my standard deviation code is not working...


When you say "not working" what do you mean? Did you try adding a few breakpoints?
Topic archived. No new replies allowed.