Input file cannot be opened

Hello guys, I'm doing a mini project about student admission data and currently my "case c" and "case d" is not running properly. Means the name and Id I try to search based on my InputData.txt is unsuccessful. Please help me to figure out any error on my coding, thank u very much.

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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

//Function to display the Menu of the program
void MainMenu ()
{
	cout << "\t\tMAIN MENU ---------------------------------" << endl;
	cout << "\t\t-------------------------------------------" << endl;
	cout << "\t\ta : Inputs" << endl;
	cout << "\t\tb : Sort the Records on Name" << endl;
	cout << "\t\tc : Search the Record on Name" << endl;
	cout << "\t\td : Search the Record based on Admission ID" << endl;
	cout << "\t\te : Save Data into text file" << endl;
	cout << "\t\tf : Load Data from text file" << endl;
	cout << "\t\tg : Quit" << endl;
	cout << "\t\t-------------------------------------------" << endl;
}

//Structure to capture user data on student's details
struct InputData
{
	string	Name;
	string	Father_Name;
	string	Address;
	string	Phone_Number;
	string	Admission_Id;
	double QualifyingTest[3];
	double Admission_Interview[3];
	double MeritScore;
	int Rank;
};

int main() 
{
	InputData Student[100];
	char MenuChoosed;
	int ArraySize;
	double Total_a, Total_b, Total_c;
	int Counter = 1;
	
	do
	{ 		
		cout << "\n\t\t\tSTUDENTS ADMISSION DATABASE     " << endl;
		cout << "\n" << endl;
		MainMenu();
		cout <<"\nChoose your menu : ";
		cin >> MenuChoosed;
		
		switch (MenuChoosed)
		{
		case 'a' :
		case 'A' :
				{
				cout << "Enter total number of student(s) : ";
				cin >> ArraySize;
			
				
				for (int i=0; i<ArraySize; i++)
				{
					cout << "\nData for student number [" << (i+1) << "]" << endl;
					cout << "***************************************************" << endl;
					cin.ignore();
					cout << "Student Name  : ";
					getline(cin, Student[i].Name);
	 
					cout << "Father's Name : ";
					getline(cin, Student[i].Father_Name);
	
					cout << "Address       : ";
					getline(cin, Student[i].Address);
	
					cout << "Phone Number  : ";
					getline(cin, Student[i].Phone_Number);
	
					cout << "Admission ID  : ";
					getline(cin, Student[i].Admission_Id);
	
					cout << "\nMarks obtained for the followings Qualifying Test" << endl;
					cout << "-------------------------------------------------" << endl;
					cout << "\tMathematics (Over 100)     : ";
					cin >> Student[i].QualifyingTest[0];
					cout <<	"\tEnglish (Over 100)         : ";
					cin >> Student[i].QualifyingTest[1];
					cout <<	"\tThinking Skills (Over 100) : ";
					cin >> Student[i].QualifyingTest[2];
		
					cout << "\nMarks obtained during the Admission Interview" << endl;
					cout << "-----------------------------------------------" << endl;
					cout << "\tPanel 1 (Over 50) : ";
					cin >> Student[i].Admission_Interview[0];
					cout << "\tPanel 2 (Over 50) : ";
					cin >> Student[i].Admission_Interview[1];
					cout << "\tPanel 3 (Over 50) : ";
					cin >> Student[i].Admission_Interview[2];
		
					cout << "*********************end**************************" << endl;
					}
				}
			break;
			
			case 'b' :
			case 'B' :
				{
				cout << "\nThe name has been successfully sorted" << endl;
				cout << "*************************************" << endl;
				cout << "\n" << endl;
				
				string temp;
        char order;
        
        cout<<"Sort by (a)ascending or (d)descending ? "<<endl;
        cin>>order;
        cout<<"\nStudent Name"<<endl;
        if (order=='a')
          {
            for (int i=0;i<ArraySize;i++)
                {
                    for (int j=0;j<ArraySize;j++)
                      {
                        if (Student[i].Name < Student[j].Name)
                          {
                            temp = Student[i].Name;
                            Student[i].Name = Student[j].Name;
                            Student[j].Name = temp;
                          }
                       }        
                }
                
           } 
        else if (order=='d')
           {
                for (int i=0;i<ArraySize;i++)
                {
                    for (int j=0;j<ArraySize;j++)
                      {
                        if (Student[i].Name > Student[j].Name)
                          {
                            temp = Student[i].Name;
                            Student[i].Name = Student[j].Name;
                            Student[j].Name = temp;
                          }
                       }
                
                 }
           }
        for (int i=0;i<ArraySize;i++)
            cout<<Student[i].Name<<endl;                             
				}
			break;
			
			case 'c' :
			case 'C' :
				{
				ifstream StreamInput;
				StreamInput.open("InputData.txt");
	
				StreamInput >> ArraySize;
	            string name;
        		bool status=false;
        		cin.ignore();
        		cout<<"\nEnter name to search : ";
        		getline(cin,name);
        		for (int i=0;i<ArraySize;i++)
          		{
     
            		if (name == Student[i].Name)
              			{     
                		cout<<"\n**********Search Successful**********"<<endl;
                		cout<<"\nTest ID : "<<Student[i].Admission_Id <<"\t"<<"\t"<<"Phone Number : "<<Student[i].Phone_Number<<endl;
                		cout<<"\nName : "<<Student[i].Name<<endl;
                		cout<<"\nFather's Name : "<<Student[i].Father_Name<<endl;
                		cout<<"\nAddress : "<<Student[i].Address<<endl;
                		status=true;
            			}
                		break; 
              	        }         
                        if (status==false)           
                        cout<<"\n**********Search Unsuccessful**********"<<endl;
                        }
			break;
			
			case 'd' :
			case 'D' :
				{
				string Admission_Id;
                bool status=false;
                cin.ignore();
                cout<<"\nEnter addmission ID to search : ";
                getline(cin,Admission_Id);
                for (int i=0;i<ArraySize;i++)
               {
                    if (Admission_Id == Student[i].Admission_Id)
                    {     
                         cout<<"\n**********Search Successful**********"<<endl;
                		 cout<<"\nTest ID : "<<Student[i].Admission_Id <<"\t"<<"\t"<<"Phone Number : "<<Student[i].Phone_Number<<endl;
                		 cout<<"\nName : "<<Student[i].Name<<endl;
                		 cout<<"\nFather's Name : "<<Student[i].Father_Name<<endl;
                		 cout<<"\nAddress : "<<Student[i].Address<<endl;
                		 status=true;
                    }
                    break; 
               }
               if (status==false)
               cout<<"\n**********Search Unsuccessful**********"<<endl;
               }
			   break;
lines 126-128,142-144: You're swapping only the student name. You have to swap the entire structure.

line 41: arraySize is an uninitialized variable.
Line 194: You're reading based on arraySize, but arraySize is garbage unless you've previous executed option a or c. Line 161 implies arraySize is written to the file, but you don;t account for it here.

line 159: Your title says you can;t open the input file, but I see no check here that the input file did or did not open successfully.

line 187: Your logic between C and D is inconsistent. In C, you read from the input file. You don't do so in D. I would suggest that you load the input file in only one place (F).

Your case branches are too big. Move your case branches into functions.


Last edited on
Topic archived. No new replies allowed.