initialization of 'fin' is skipped by 'case' label

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nunii07
    New Member
    • Jan 2015
    • 1

    initialization of 'fin' is skipped by 'case' label

    Please! Help! This program was running at first but when I started to change the couts and cins to fouts and fins (in order for them to be save in a file directory), it shows a lot of errors such as:

    initialization of 'fin' is skipped by 'case' label
    'std::ifstream fin': redefinition

    Here is the code of the program:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    struct file{
    	int fnum;
    	char fname[50], lname[50];
    	char cnum[50];
    	int balance, total;
    };
    
    file f;
    //int i, list,ans, searchnum, delnum, x, y;
    int ans, list, i, searchnum, delnum, y;
    int cons, es, minsur, majsur;
    char others[50], name[50], yon, noy;
    int pay, oprice, balpaid;
    
    void newLine();
    void line(char, int);
    void filedirectory();
    void deletefile();
    void procedure();
    void addfile();
    
    
    
    int main()
    {
    	/*ofstream fout;
    	fout.open("d:\\hayamieyecenter.txt");*/
    	int choice, list=1;
    	f.total=0;
    	while(1){
    		system("cls");
    	cout << endl;
    	cout << "Welcome to Hayami Eye Specialist Center";
    	cout << "\n" << endl;
    	line('=',40);
    	cout << "            1. Add New File            \n";
    	cout << "            2. Edit File              \n";
    	cout << "            3. Search File             \n";
    	cout << "            4. Delete File             \n";
    	cout << "            5. View Files              \n";
    	cout << "            6. Exit                \n";
    	line('=',40);
    	cout << endl;
    	cout << "What would you like to do? ";
    	cin >> choice;
    	switch (choice)
    	{
    		case 1: //file cannot be saved
    			FILE *fp;
    			fp=fopen("d:\\myfiles\\sample2.txt","w");
    			ifstream fin;
    			fin.open("d:\\hayamieyecenter.text");
    			system("cls");
    			//addfile();
    			cout << "ADD NEW FILE\n";
    			line ('-', 26);
    			cout << "Enter Patient Information:\n";
    			cout << "File Number " << setprecision(3) << list;
    			cout << "\nFirst Name: ";
    			fin.getline(f.fname,50);
    			fputs(f.fname,fp);
    			newLine();
    			cout << "Last Name: ";
    			fin.getline(f.lname,50);
    			cout << "Contact Number: ";
    			fin >> f.cnum;
    			cout << endl;
    			procedure();
    			//addfile();
    			break;
    		case 2:
    			ifstream fin;
    			fin.open("d:\\hayamieyecenter.txt");
    			system("cls");
    			cout << "EDIT FILE\n";
    			line ('-', 25);
    			cout << "Enter File # to Edit: ";
    			cin >> i;
    			//ofstream fout;
    			//fout.open("d:\\hayamieyecenter.txt");
    			newLine();
    			if (i=list)
    			{
    			cout << "Edit First Name: ";
    			fin.getline(f.fname,50);
    			cout << "Edit Last Name: ";
    			fin.getline(f.lname,50);
    			cout << "Edit Contact Number: ";
    			fin >> f.cnum;
    			cout << "Balance: " << f.balance << "\nEnter amount to be paid: ";
    			fin >> balpaid;
    			f.balance-=balpaid;
    			fin >> f.balance;
    			cout << "are there more transactions made (Y/N) ? " << endl;
    			cin >> yon;
    			if (yon =='y'||yon =='Y')
    				procedure();
    			if (yon == 'n'||yon == 'N')
    				break; 
    			}
    		break;
    		case 3:{
    			ofstream fout;
    		fout.open("d:\\hayamieyecenter.txt");
    		system("cls");
    			cout << "SEARCH FILE\n";
    			line ('-', 25);
    		cout << "1.First Name\n";
    		cout << "2.Last Name\n";
    		cout << "3.Contact Number\n";
    		cout << "Select a number to search: ";
    		cin >> searchnum;
    						switch (searchnum){
    											case 1:
    								cout << "Enter First Name to be searched: ";
    								fin.getline(name,50);
    								/*for (int a=0;a<=list;a++)
    								{*/
    									if (name == f.fname)
    									{
    								fout << endl;
    								fout << "  " << list << setw(15);
    								fout << f.fname << setw(25);
    								fout << f.lname << setw(25);
    								fout << f.cnum;
    								/*}*/
    								}
    								break;
    											case 2:
    								fout << "Enter Last Name to be searched: ";
    								fin.getline(name,50);
    								/*for (int a=0;a<=list;a++)
    								{*/
    								if (name == f.lname)
    								{
    								fout << endl;
    								fout << "  " << i + 1 << setw(15);
    								fout << f.fname << setw(25);
    								fout << f.lname << setw(25);
    								fout << f.cnum;
    								}
    								//}
    								break;
    										case 3:
    								fout << "Enter Contact Number to be searched: ";
    								fin.getline(name,50);
    								/*for (int a=0;a<=list;a++)
    								{*/
    								if (name == f.cnum)
    								{
    								fout << endl;
    								fout << "  " << i + 1 << setw(15);
    								fout << f.fname << setw(25);
    								fout << f.lname << setw(25);
    								fout << f.cnum;
    								}
    								//}
    								break;
    										default:
    								fout << "Invalid choice";
    								system("pause>0");
    								}
    						break;}
    		case 4: //not yet fixed but claiming for fin
    			{deletefile();
    			break;}
    		case 5: //NOT YET FIXED BUT CLAIMING FOR FIN
    			{filedirectory();
    			system("pause>0");
    			break;}
    		case 6:
    			exit(1);
    		default:
    			cout << "Invalid choice, please try again: ";
    			system("pause>0");
    	} //choice
    	} //while(1)
    } //int main()
    
    void procedure()
    {
    	ifstream fin;
    	fin.open("d:\\hayamieyecenter.txt");
    	cons = 3000;
    	es = 4000;
    	minsur = 60000;
    	majsur = 800000;
    	cout << "1. Consultation\n";
    			cout << "2. Eye Screening\n";
    			cout << "3. Minor Surgery\n";
    			cout << "4. Major Surgery\n";
    			cout << "5. Others\n";
    			cout << "Select a Procedure: ";
    			cin >> ans;
    			switch (ans){
    			case 1:
    				cout << "Price: " << cons;
    				f.total+=cons;
    				break;
    			case 2:
    				cout << "Price" << es;
    				f.total+=es;
    				break;
    			case 3:
    				f.total+=minsur;
    				cout << "Price" << minsur;		
    				break;
    			case 4:
    				f.total+=majsur;
    				cout << "Price" << majsur;
    				break;
    			case 5:
    				cout << "Please Enter the desired procedure: ";
    				fin.getline(others,50);
    				cout << "Price: ";
    				fin >> oprice;
    				f.total+=oprice;
    			default:
    				cout << "Invalid choice";
    				system("pause>0");
    			}
    			
    			cout << "\nTotal Price:" << f.total << endl;
    			cout << "Payment: ";
    			fin >> pay;
    			if (pay < f.total) {
    				f.balance+=f.total-pay;
    				//fputc(f.balance,);
    			}
    } //procedure()
    
    void filedirectory()
    {
    			ofstream fout;
    			fout.open("d:\\hayamieyecenter.txt");
    			system("cls");
    			cout << "FILE DIRECTORY\n";
    			line ('-', 80);
    			system("COLOR 5");
    			cout << "File No." << setw(15)
    			<< "First Name" << setw(25)
    			<< "Last Name" << setw(25)
    			<< "Contact No."
    			<<endl;
    			line('-', 80);
    
    			fout.setf(ios::fixed);
    			fout.setf(ios::showpoint);
    			fout.precision(2);
    
    			/*for (int x=0; x < list; x++){*/
    			fout << endl;
    			fout << "  " << i + 1 << setw(15);
    			fout << f.fname << setw(25);
    			fout << f.lname << setw(25);
    			fout << f.cnum << endl;
    			fout << "Transaction: " << setw(40);
    			fout << f.total << setw(15);
    			fout << "Balance: " << f.balance;
    } // filedirectory
    
    void deletefile()
    {
    		ifstream fin;
    		fin.open("d:\\hayamieyecenter.txt");
    		system("cls");
    			cout << "DELETE FILE\n";
    			line ('-', 25);
    	bool is_ok = false;
    	while(! is_ok)
    	{
    	cout << "\nSelect a file number to delete: ";
    	cin >> delnum;
    
    	if (delnum > list)
    		cout << "The file number does not yet exist";
    	else
    		break;
    	}
    		//y = delnum -1;
    			delnum = list;
    			fin.clear();
    			fin.ignore();
    			cout << endl;
    			cout << "  " << list << setw(15);
    			cout << f.fname << setw(25);
    			cout << f.lname << setw(25);
    			cout << f.cnum;
    			cout << "Are you sure (Y/N)? ";
    			cin >> noy;
    			if (noy == 'y' || noy == 'Y')
    			{
    				//for (int x=y; x<list; x++)
    				//{
    				//	/*f.fname = f[list+1].fname;
    				//	f[list].lname = f[x+1].lname;
    				//	f[x].cnum = f[x+1].cnum;
    				//	f[x].balance = f[x+1].balance;
    				//	f[x].total =  f[x+1].total;*/
    				//}
    				--list;
    				
    				/*for (int x = 0; x <list; x++)
    				{
    					fout << f[x].fname << "," <<f[x].lname << "," << 
    				}*/
    			}
    
    		system("pause>0");
    } //deletefile()
    
    void addfile()
    {
    	ofstream fout;
    	fout.open("d:\\hayamieyecenter.txt");
    	list++;
    } //addfile
    
    
    void line(char ch, int ctr)
    {
    	for (int i = 0; i < ctr; i++) {
    		cout << ch;
    	}
    	cout << endl;
    }
    
    void newLine()
    {
    	char s; do{
    		cin.get(s);
    	} while (s != '\n');
    }
    It'll be a lot of help. thank you so muchhhhh!!! ^_^
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The variable fin is defined in case 2 but you use it in case 1.

    You will need to move the definition of fin so it is visible to all the cases.

    Comment

    Working...