Hi All
I have copied and pasted in the a section of my code, it is a menu system which asks which type of fuel the car is using, if entered 1 it displays petrol, 2 diesel and 3 hybrid. If any other number is entered it loops back round and asks you to please reselect. that all works fine, the problem comes when you enter a character, i.e a the program goes crazy and prints out endless lines of rubish. Can someone please tell me how to fix this.
Many thanks
steve
#include <fstream.h>
#include <iostream>
#include <string.h>
class Car_Details //Name of class is Car_Details
{
int Fuel;
int Price3;
char Fuel1[8];
public:
void CarFuel (void)
{
int finished = 0;
cout << "\nPlease Select Fuel Type of Car: i.e Enter 1 for Petrol\n" // Selecting fuel type
<< " 1. Petrol: \n"
<< " 2. Diesel: \n"
<< " 3. Hybrid: \n";
do
{
cin >> Fuel;
switch( Fuel )
{
case 1:
cout << "Fuel type selected is Petrol\n";
{
Price3 = (float) 0.8;
strcpy(Fuel1, "Petrol");
finished = 4;
}
break ;
case 2:
cout << "Fuel type selected is Diesel\n";
{
Price3 = (float) 0.9;
strcpy(Fuel1, "Diesel");
finished = 4;
}
break;
case 3:
cout << "Fuel type selected is Hybrid\n";
{
Price3 = 1;
strcpy(Fuel1, "Hybrid");
finished = 4;
}
break;
default:
cout <<"Invalid key press \n"
<< "Please reselect fuel type: \n "
<< "1. Petrol: \n"
<< " 2. Diesel: \n"
<< " 3. Hybrid: \n";
break;
}
}
while (finished !=4 );
}
void Show_Car(void)
{
cout <<"\nFuel type is: "<<Fuel1;
cout <<endl;
}
int test(void)
{
char NewCar;
Car_Details Car;
fstream carinfo;
carinfo.open("cardetails.dat", ios::trunc | ios:ut | ios::in);
do
{
cout << "\nEnter Car details";
Car.CarFuel();
carinfo.write( (char*)&Car, sizeof(Car) );
cout << "\nDo you want to enter another Cars Details? (Yes or No?) ";
cin >> NewCar;
}
while (NewCar == 'Y' || NewCar == 'y');
carinfo.seekg(0);
carinfo.read( (char*)&Car, sizeof(Car) );
while (!carinfo.eof())
{
cout << "\nCar Prenimum";
Car.Show_Car();
carinfo.read( (char*)&Car, sizeof(Car) ); //Read from file
}
return(0);
}
};
int main()
{
Car_Details car;
car.test();
return(0);
}
I have copied and pasted in the a section of my code, it is a menu system which asks which type of fuel the car is using, if entered 1 it displays petrol, 2 diesel and 3 hybrid. If any other number is entered it loops back round and asks you to please reselect. that all works fine, the problem comes when you enter a character, i.e a the program goes crazy and prints out endless lines of rubish. Can someone please tell me how to fix this.
Many thanks
steve
#include <fstream.h>
#include <iostream>
#include <string.h>
class Car_Details //Name of class is Car_Details
{
int Fuel;
int Price3;
char Fuel1[8];
public:
void CarFuel (void)
{
int finished = 0;
cout << "\nPlease Select Fuel Type of Car: i.e Enter 1 for Petrol\n" // Selecting fuel type
<< " 1. Petrol: \n"
<< " 2. Diesel: \n"
<< " 3. Hybrid: \n";
do
{
cin >> Fuel;
switch( Fuel )
{
case 1:
cout << "Fuel type selected is Petrol\n";
{
Price3 = (float) 0.8;
strcpy(Fuel1, "Petrol");
finished = 4;
}
break ;
case 2:
cout << "Fuel type selected is Diesel\n";
{
Price3 = (float) 0.9;
strcpy(Fuel1, "Diesel");
finished = 4;
}
break;
case 3:
cout << "Fuel type selected is Hybrid\n";
{
Price3 = 1;
strcpy(Fuel1, "Hybrid");
finished = 4;
}
break;
default:
cout <<"Invalid key press \n"
<< "Please reselect fuel type: \n "
<< "1. Petrol: \n"
<< " 2. Diesel: \n"
<< " 3. Hybrid: \n";
break;
}
}
while (finished !=4 );
}
void Show_Car(void)
{
cout <<"\nFuel type is: "<<Fuel1;
cout <<endl;
}
int test(void)
{
char NewCar;
Car_Details Car;
fstream carinfo;
carinfo.open("cardetails.dat", ios::trunc | ios:ut | ios::in);
do
{
cout << "\nEnter Car details";
Car.CarFuel();
carinfo.write( (char*)&Car, sizeof(Car) );
cout << "\nDo you want to enter another Cars Details? (Yes or No?) ";
cin >> NewCar;
}
while (NewCar == 'Y' || NewCar == 'y');
carinfo.seekg(0);
carinfo.read( (char*)&Car, sizeof(Car) );
while (!carinfo.eof())
{
cout << "\nCar Prenimum";
Car.Show_Car();
carinfo.read( (char*)&Car, sizeof(Car) ); //Read from file
}
return(0);
}
};
int main()
{
Car_Details car;
car.test();
return(0);
}