Please help me with this Run-Time Cjeck error

I've written a program in order to learn C++ but keeps giving me this error when I enter my choice.

Run-Time Check Failure #3 - The variable 'stdDetailsStruct_1' is being used without being initialized.

I don't know how to solve this I've checked and rechecked my code but can't understand what I've done wrong can some please help me with this.

My full code as follows

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// e.cpp : Defines the entry point for the console application.


/********************************************************************************

Crated by Sadaruwan Samaraweera
In behalf of New Eastern Campus
Coding Start Date 22nd April 2013

*********************************************************************************/

#include <iostream>

#include <string>

 

using namespace std;

 
//Struct Declaration for stdDetails
struct stdDetails {

   	int stdNum; //Student Registration Number
    char fName[25]; // Student First Name
    char lName[25]; // Student Last Name
	char resAdd[50]; //Resident Address
	char dBirth[10]; // Student Date Of Birth
	int stdAge; // Student Age
	char nicNum[10]; // Sudent National ID number
	int conNum; // Student Contact Number
    char dateEnroll[10]; // Student Enrollment date
	char corsSelect[25]; // Course Selected
	char payMethod[10]; // Fee payment method
    double amtPaid; // Amount Student Paid for the course    
    char bksIssue[25]; //Issued Books

};


//Declartion of methods usend in this program
void ui(); //Method for displaying the UI
void output(stdDetails  stdDetailsStruct_1);
stdDetails  enterDetails (stdDetails stdDetailsStruct_1); //Method for entering student details to the prgoram
void entLine(); //To make interface more nice
void stars(); //Draws a line of stars to make CLI look more nice

//Declaration of i as a Variable for the counter
int i=0;

//Start of main method
int main(){

	ui();
    return 0;
}

//CLI method start from here on
void ui (){

    int  choice;
    stdDetails  stdDetailsStruct_1;
	stdDetails  stdDetailsStruct_2[1000];
	char ansr = 'y';
	int search = 0;

	stars();
	cout<<"		Welcome to New Eastern Campus "<<endl;
	cout<<endl;
	stars();
	entLine();

//Beginning of the while loop which will display the options that user have to enter
		do{

			cout<<"		Press Number [1] for New Student Registration  : "<<endl;
			cout<<"		Press Number [2] to Search Student Details : "<<endl;
			cout<<"		Press Number [3] to Delete Student Details : "<<endl;
			cout<<"		Press Number [4] to View Student Details: "<<endl;
			entLine();
			cout<<"		Enter your choice here: ";
			cin>>choice;
			entLine();
//Start of the switch case for every choice selected will perform an action
			switch(choice){

//This Case will add the details entered to the stdDetails array
				case 1:
					stdDetailsStruct_1 = enterDetails(stdDetailsStruct_1);
					stdDetailsStruct_2[i]=stdDetailsStruct_1;
					i++;
				break;

//This Case will search the array for a given keyword 
				case 2:{
					stdDetails stdDetailsStruct_0[1000];
					int kWord;
					stdDetails  stdFindRes;
					cout<<"Enter the Student Registration Number of the Student: ";
					cin>>kWord;
						for(int x=0;x<i;x++){
						stdFindRes = stdDetailsStruct_0[x];
						if(kWord == stdFindRes.stdNum){
								search=1;
							break;
							}
						}
						if(search==1){
							output(stdFindRes);
						}else{
							cout<<"Student details not found please try again."<<endl;
							search = 0;
						}
					   }
				break;

//This case will delete the students details form the array 
				case 3:{
					search=0;
					int delStd,m;
					stdDetails  stdDel;
					cout<<"Enter the Registration Numeber of the student: ";
					cin>>delStd;
						for(m=0;m<i;m++){
						stdDel = stdDetailsStruct_2[m];
							if(delStd==stdDel.stdNum){
								search=1;
							break;
							}
						}
						if(search==1){
							for(int z=m;z<i;z++){
								stdDetailsStruct_2[z]=stdDetailsStruct_2[z+1];
								i--;
							}
						}else{
							cout<<"Std  not found"<<endl;
						}
					   }
				break;
//This case will display the student details
				case 4:
					cout<<"=================================="<<endl<<endl;;
					for(int x=0;x<i;x++){
						stdDetails  stdView = stdDetailsStruct_2[x];
						output(stdView);
						cout<<endl;
						cout<<"***********************************"<<endl;
						cout<<endl;
					}
					cout<<"=================================="<<endl;
				break;

//The default message if  any thing goes wrong
				default:
					cout<<"Invalid Option Please Enter a Valid Entry"<<endl;
					break;
				}

//The option case list ends from here
				entLine();
				cout<<"Continue [y/n]: ";
				cin>>ansr;
				entLine();
			}while(ansr == 'y'||ansr == 'Y');

//While Loop Ends here

			cout<<"Thank You"<<endl;
}
//Ending of the UI system

//This method is used to decorate the interface
void entLine(){
	cout<<endl;
    cout<<endl;
}
//Method End

//Data Display Method Starts from here
void output(stdDetails  stdDetailsStruct_1){
    cout<<"Student Registration Number: "<<stdDetailsStruct_1.stdNum<<endl;
    cout<<"Student First Name: "<<stdDetailsStruct_1.fName<<endl;
    cout<<"Student Last Name: "<<stdDetailsStruct_1.lName<<endl;
	cout<<"Student Resident Address: "<<stdDetailsStruct_1.resAdd<<endl;
	cout<<"Student Date Of Birth: "<<stdDetailsStruct_1.dBirth<<endl;
	cout<<"Student Age: "<<stdDetailsStruct_1.stdAge<<endl;
	cout<<"Student National ID number: "<<stdDetailsStruct_1.nicNum<<endl;
	cout<<"Student Contact Number: "<<stdDetailsStruct_1.conNum<<endl;
	cout<<"Student Enrollment Date: "<<stdDetailsStruct_1.dateEnroll<<endl;
	cout<<"Selected Course: "<<stdDetailsStruct_1.corsSelect<<endl;
	cout<<"Payment Mthod: "<<stdDetailsStruct_1.payMethod<<endl;
	cout<<"Paid Amount: "<<stdDetailsStruct_1.amtPaid<<endl;
	cout<<"Books Issued: "<<stdDetailsStruct_1.bksIssue<<endl;
}
//Data Display Method Ends

//Data Entery Method start from here
stdDetails  enterDetails (stdDetails  stdDetailsStruct_1){

	cout<<"Enter Student Registration Number : ";
	cin>>stdDetailsStruct_1.stdNum;
	cout<<"Enter Sutdent Firs Name : ";
	cin>>stdDetailsStruct_1.fName;
	cout<<"Enter Student Last Name : ";
	cin>>stdDetailsStruct_1.lName;
	cout<<"Enter Student Resident Address : ";
	cin>>stdDetailsStruct_1.resAdd;
	cout<<"Enter Student DoB : ";
	cin>>stdDetailsStruct_1.dBirth;
	cout<<"Enter Student Age : ";
	cin>>stdDetailsStruct_1.stdAge;
	cout<<"Enter Student NIC number : ";
	cin>>stdDetailsStruct_1.nicNum;
	cout<<"Enter Student Contact number : ";
	cin>>stdDetailsStruct_1.conNum;
	cout<<"Enter Student Enrollment Date : ";
	cin>>stdDetailsStruct_1.dateEnroll;
	cout<<"Selected Course : ";
	cin>>stdDetailsStruct_1.corsSelect;
	cout<<"Payment Method : ";
	cin>>stdDetailsStruct_1.payMethod;
	cout<<"Amount Paid : ";
	cin>>stdDetailsStruct_1.amtPaid;
	cout<<"Books Issued : ";
	cin>>stdDetailsStruct_1.bksIssue;
	return stdDetailsStruct_1;
}
//Data Entry  Method End

//CLI Line of stars
void stars(){
for (int i=1; i<=50; i++) {
	cout << "*"<<endl;
}
}
> I don't know how to solve this I've checked and rechecked my code
> but can't understand what I've done wrong

What is wrong is easy to understand.
The variable 'stdDetailsStruct_1' is being used without being initialized.


To find out where the error is occurring, and then where you have forgotten to initialise the variable, run the program with the debugger.
Typically, from an IDE, Menu => Debug => ....
You cannot invoke stdDetailsStruct_1 = enterDetails(stdDetailsStruct_1); since stdetailsStruct_1 is not initialized.
Here is how you initialize a structure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>

struct test{
  int a;
  int b;
  char x[25];
  char y[25];
  
  test()
  {
    a=0;
    b=0;
    std::strcpy(x,"x");
    std::strcpy(y,"");
  }
};

int main()
{
  test p;
  std::cout<<p.a<<p.b<<p.y<<p.x<<std::endl;
  return 0;
}
This is the place where this error occurs.

1
2
3
4
5
6
//This Case will add the details entered to the stdDetails array
				case 1:
					stdDetailsStruct_1 = enterDetails(stdDetailsStruct_1);
					stdDetailsStruct_2[i]=stdDetailsStruct_1;
					i++;
				break;


I think I've initialized all the structs
I think I've initialized all the structs


Nope. stdDetailsStruct_1 has not been assigned any meaningful value when it is fed to enterDetails. Looking at the implementation of enterDetails one wonders why it takes a parameter.

1
2
3
4
5
6
//Data Entery Method start from here
stdDetails enterDetails (){
        stdDetails stdDetailsStruct_1 ;
	cout<<"Enter Student Registration Number : ";
	cin>>stdDetailsStruct_1.stdNum;
        // ... 


would make more sense and then the case would become:

1
2
3
4
				case 1:
					stdDetailsStruct_1 = enterDetails();
					stdDetailsStruct_2[i++]=stdDetailsStruct_1;
				break;





I'm very sorry to be this dumb guys I've just started to learn C++ more of a PHP person any way I tried "CIRE" solution then it started to give fatal error so reverted back to the normal one.

Is there a any other way to initialize struct ats15 his way looks good but if some one can how to do the char is that way is correct.
Last edited on
Add a default constructor to your struct. This will be called when ever your code creates an instance of the struct, and will initialise the member variables:

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
#include <string> // for memset

//Struct Declaration for stdDetails
struct stdDetails
{
	// default constructor will automatically
	// initialise member variables
	stdDetails()
		: stdNum(0)
		, stdAge(0)
		, conNum(0)
		, amtPaid(0)
	{
		memset(fName, 0, 25);
		memset(lName, 0, 25);
		memset(resAdd, 0, 50);
		memset(dBirth, 0, 10);
		memset(nicNum, 0, 10);
		memset(dateEnroll, 0, 10);
		memset(corsSelect, 0, 25);
		memset(payMethod, 0, 10);
		memset(bksIssue, 0, 25);
	}

	int stdNum;	//Student Registration Number
	int stdAge;	// Student Age
	int conNum;	// Student Contact Number
	double amtPaid;	// Amount Student Paid for the course
	char fName[25];	// Student First Name
	char lName[25];	// Student Last Name
	char resAdd[50];	//Resident Address
	char dBirth[10];	// Student Date Of Birth
	char nicNum[10];	// Sudent National ID number
	char dateEnroll[10]; // Student Enrollment date
	char corsSelect[25]; // Course Selected
	char payMethod[10];// Fee payment method    
	char bksIssue[25];	//Issued Books
};
Last edited on
ajh32 Thank you very much for teaching me thank you all for the help I'm very sorry to trouble you with my dumb questions but you have helped me a lot thank you again.
Topic archived. No new replies allowed.