Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert from public class to struct?

Status
Not open for further replies.

dejah0308

Programmer
Jul 3, 2022
1
0
0
MY
Hi, so i try to convert code that has public class to struct, so for the initial code it will be like this:

Code:
#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#include<fstream>
class car
{
	public:
	int vno;
	float count;
	char  dname[15],x,l[50];
	void input()
	{
		system("CLS");
			int d;
		cout<<"\n\n\t\tEnter the name of driver : ";
		cin>>dname;
		cout<<"\n\n\t\tEnter the car no : ";
		cin>>vno;
		cout<<"\n\n\t\tEnter the no of hours of stay : ";
		cin>>count;
		cout<<"\n\n\t\tEnter the time slot : ";
		cin>>l;
		if(d<20&&count<8)
   			{
   				cout<<"\n\n\t\tParking Available! You can park your car. ";
   			}
   			else
   				cout<<"\n\n\t\t Parking not possible!!";
   			cout<<"\n\n\t\tCar got parked!!";
		
	}
	void cal()
	{
			
		cout<<"\n\t\n\tAre you a V.I.P: y/n ";
		cin>>x;
		if(x=='y')
	{
		system("CLS");
		cout<<"\n\t\n\tThe total expense during parking  ";
		cout<<count*20<<" rupees";
	}
	else 
	{
		system("CLS");
		cout<<"\n\t\n\tThe total expenses during parking  ";
		cout<<count*25<<" rupees";
		
	}
}	
	void output()
	{
		cout<<"\n\n\t\tThe Driver Name : "<<dname<<endl;
		cout<<"\n\n\t\tThe Car No : "<<vno<<endl;
		cout<<"\n\n\t\tThe Hours Of Stay : " <<count;
		cout<<"\n\n\t\tThe Time Slot Of Parking : " <<l;
	
	}
	

		
}a,a1;
//brought to you by code-projects.org
void delete_record()
{
	int n;
	system("CLS");
	cout<<"\n\n\t\tEnter the car no you want to get depart : ";
	cin>>n;
	ifstream inFile;
    inFile.open("parking3.dat", ios::binary);
	ofstream outFile;
    outFile.open("temp.dat", ios::out | ios::binary);
    while(inFile.read((char*)&a, sizeof(a)))
    {
        if(a.vno!= n)
        {	
        	
            outFile.write((char*)&a, sizeof(a));
        }
         
    }
    cout<<"\n\n\t\t Parking Record Deleted";

    inFile.close();
    outFile.close();
    
    remove("parking3.dat");
    rename("temp.dat", "parking3.dat");
}

int main()
{
	int login();
    login();
	int choice,vno1,d;
	while(1) {
	system("CLS");

     cout << "\n\n\t\t ====== CAR PARKING RESERVATION SYSTEM ======";
     cout <<"\n\n                                          ";
     cout<<" \n\t\t\t======================";
     cout << "\n\n\t\t\t  1. Arrival of a Car";
     cout << "\n\n\t\t\t  2. Total no of cars Arrived";
     cout << "\n\n\t\t\t  3. Total parking charges of all cars with details";
     cout << "\n\n\t\t\t  4. Departure of the car";
     cout << "\n\n\t\t\t  5. Exit Program";
     cout<<" \n\t\t\t======================";
     cout << "\n\n";
     cout << "\t\t\t Select Your Choice ::";
     cin>>choice;
     switch(choice)
     {
      case 1 :
        {		
        		char f;
        		cout<<"\n\n\t\t==Do You Want to continue==y/n ";
        		cin>>f;
        		if(f=='y')
        		{
        		
				ofstream f1("parking3.dat",ios::binary|ios::out|ios::app);
                a.input();
                f1.write((char*)&a,sizeof(a));
        }
            	break;
        }
       case 2 :
           {
           	system("CLS");
  		   cout << "\n\n\t\t=== View the Records in the Parking Database ===";
           cout << "\n";
           ifstream infile("parking3.dat",ios::binary|ios::in);
           int d=1;
           while (infile.read((char *)&a,sizeof (a)))
		   {
		system("CLS");
           cout << "\n";
           cout<<"\n\n\tThe Car position in parking lot : "<<d;
           d++;
           a.output();
           cout<<"\n";
           cout<<"\n\n\n";
           system("PAUSE");
           cout<<endl;
       	   }
       
           break;
       	   }
       	case 4:	
		   {
		   
		   delete_record();
		   break;
	}
    	case 3:
		{
				ifstream infile("parking3.dat",ios::binary|ios::in);
           while (infile.read((char *)&a,sizeof (a)))
		   {
             cout << "\n";
			 a.cal();
			 a.output();
			 cout<<"\n";
			 cout<<endl;
			 
       		}
       		break;
   }
   		
   		case 5: 
		   {
		   	system("CLS");
		   	cout<<"\n\n\n\t\t\tBrought To You By: code-projects.org \n";
		   exit(0);
   			break;
   		}
   		default : 
		   {
		   cout<<"\n\n\t\t Invalid input";
			cout<<"\n\n\t\tPress Enter to continue";
			 
   	
   	}
   }
getch();
}}
//cpo
int login(){
   string pass ="";
   char ch;
   cout <<"\n\n\n\n\n\n\n\t\t\t\t\tCar Parking Reservation System Login";
   cout << "\n\n\n\n\n\n\n\t\t\t\t\t\tEnter Password: ";
   ch = _getch();
   while(ch != 13){//character 13 is enter
      pass.push_back(ch);
      cout << '*';
      ch = _getch();
   }
   if(pass == "pass"){
      cout << "\n\n\n\n\t\t\t\t\t  Access Granted! Welcome To Our System \n\n";
      system("PAUSE");
      
   }else{
      cout << "\n\n\n\n\t\t\t\t\tAccess Aborted...Please Try Again!!\n";
      system("PAUSE");
      system("CLS");
      login();
   }
}

however, I try to convert to struct directly but the choice 2,3 and 4 doesn't work :(
Code:
//password admin system: pass
#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#include<fstream>
#include<cstdlib>
#include<process.h>
class car
{
	public: //public member function can access any private, protected and public member of it's class//
	int VehicleNo;
	float Hours;
	char  DriverName[30],Time[50],VIP;
	void input() //fetch data needed from the user
	{
		system("cls"); //to clear the screen
		int i;
		cout<<"\n\n\t\tEnter the name of driver : ";
		cin>>DriverName;
		cout<<"\n\n\t\tEnter the car no : ";
		cin>>VehicleNo;
		cout<<"\n\n\t\tEnter the no of hours of stay : ";
		cin>>Hours;
		if(i<20 && Hours<8)
   	     {
   		     cout<<"\n\n\t\tParking slot available. You can park your car.";
   		 }
   		else
   			cout<<"\n\n\t\t Sorry, parking slot not available for your car.";
   			
		
	}
 
 void cal() //function to calculate parking fees
	{
			
		cout<<"\n\t\n\tAre you registered as a V.I.P?: y/n ";
		cin>>VIP;
		if(VIP=='y')
	  {
		system("cls");
		cout<<"\n\t\n\t[VIP] The total of parking fees: ";
		cout<<Hours*20<<" ringgit"; //calculate payment for vip member
	  }
	else 
	  {
		system("cls");
		cout<<"\n\t\n\t[NON-VIP] The total of parking fees: ";
		cout<<Hours*25<<" ringgit"; //calculate payment for non-vip member
		
	  }
    }	
	void output() //details statement to the user
	{
		cout<<"\n\n\t\tThe Driver Name : "<<DriverName<<endl;
		cout<<"\n\n\t\tThe Car No : "<<VehicleNo<<endl;
		cout<<"\n\n\t\tThe Hours Of Stay : " <<Hours;
	}
			
}

a,a1;
void delete_record() //function to delete record after user depart
{
	int depart;
	system("cls");
	cout<<"\n\n\t\tEnter the car no you want to get depart : ";
	cin>>depart;
	ifstream inFile; //read from the file
    inFile.open("parking3.dat", ios::binary); //makes sure the data is read/written without translating new line characters//
	ofstream outFile; //write to the file
    outFile.open("temp.dat", ios::out | ios::binary);  
    while(inFile.read((char*)&a, sizeof(a)))
    {
        if(a.VehicleNo!= depart)
        {	
        	
            outFile.write((char*)&a, sizeof(a));
        }
         
    }
    cout<<"\n\n\t\t Deleting your parking record...Successful.";

    inFile.close(); //close the file
    outFile.close();
    
    remove("parking3.dat"); //remove old data file
    rename("temp.dat", "parking3.dat"); //rename new data file
}

int main() // main function for the menu 
{
	int login();
    login(); // using login function
	int choice,VehicleNo1,i;
	while(1) 
	{
	 system("cls"); //clear the screen
	     //printing menu page statement
	     cout << "\n\n\t\t ====== CAR PARKING RESERVATION SYSTEM ======";
	     cout <<"\n\n                                          ";
	     cout<<" \n\t\t\t======================";
	     cout << "\n\n\t\t\t  1. Arrival of a Car"; //input new data from new car
	     cout << "\n\n\t\t\t  2. Total no of cars Arrived"; //view total cars arrived
	     cout << "\n\n\t\t\t  3. Total parking charges of all cars with details"; //view payment data for all car
	     cout << "\n\n\t\t\t  4. Departure of the car"; //input car departure
	     cout << "\n\n\t\t\t  5. Exit Program"; //exit the program
	     cout<<" \n\t\t\t======================";
	     cout << "\n\n";
	     cout << "\t\t\t Select Your Choice ::"; //admin input choice either 1,2,3,4 or 5
	     cin>>choice;
     switch(choice) //using switch structure
     {
      case 1 : //choice 1: car arrival
        {		
		    char f;
		    cout<<"\n\n\t\t==Do You Want to continue==Yes(y)/No(n) ";
		    cin>>f;
		    if(f=='y')
		    {
		        ofstream f1("parking3.dat",ios::binary|ios::out|ios::app);
			    a.input();
			    f1.write((char*)&a,sizeof(a));
		    }
		    break;
        }
       
	   case 2 : //choice 2: viewing total car arrived
        {
           system("cls");
  		   cout << "\n\n\t\t== Viewing Record in the Parking Database ==";
           cout << "\n";
           ifstream infile("parking3.dat",ios::binary|ios::in); //reading data from the file
           int i=1;
           while (infile.read((char *)&a,sizeof (a)))
		   {
			   system("cls");
	           cout << "\n";
	           cout<<"\n\n\tThe Car position in parking lot : "<<i; //determine car position in the parking
	           i++; 
	           a.output();
	           cout<<"\n";
	           cout<<"\n\n\n";
	           system("PAUSE");
	           cout<<endl;
       	   }
       
           break;
       	}	
	   
	   case 3: //choice 3: details of parking charges of all cars
		{
		   ifstream infile("parking3.dat",ios::binary|ios::in); //reading from the file
           while (infile.read((char *)&a,sizeof (a)))
		   {
             cout << "\n";
			 a.cal(); //function call, which for calculating the payment
			 a.output();
			 cout<<"\n";
			 cout<<endl;	 
       	   }
       		break;
       	
    
        }
   		
		case 4:	//choice 4: car departure
		{
			delete_record(); //function call to delete the record of car departing
			break;
	    }
	    
	    case 5: //exit the program
		{
		   	system("CLS");
		   	exit(0);
   			break;
   	    }
		   
		default : //default option if admin choose option other than 1,2,3,4 or 5
	    {
		   cout<<"\n\n\t\t Invalid input";
		   cout<<"\n\n\t\tPress Enter to continue";
	    }
   	
   	 }
     getch();
     
     
	}
}

//function admin login menu 
int login()
{
   string pass ="";
   char ch;
   cout <<"\n\n\n\n\n\n\n\t\t\t\t\tCar Parking Reservation System Login";
   cout << "\n\n\n\n\n\n\n\t\t\t\t\t\tEnter Password: "; // admin enter the password: pass
   ch = _getch();
      
	while(ch != 13) //character 13 is enter
	   { 
	      pass.push_back(ch);
	      cout << '*'; //password entered will be in **** form
	      ch = _getch();
	   }
	   
	if(pass == "pass") //the password entered match
	   { 
	      cout << "\n\n\n\n\t\t\t\t\tAccess Granted! Welcome To The System \n\n";
	      system("PAUSE");
	      
	   }
	   
	else  //the password entered not match
	  { 
	      cout << "\n\n\n\n\t\t\t\t\tAccess Aborted. Please Try Again\n";
	      system("PAUSE");
	      system("cls");
	      login();
	  }
}

how to make it work, please help me :(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top