C++ Help - Menu problems

Discussion in 'Programmer's Corner' started by stevy123, Apr 5, 2008.

  1. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    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::eek: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);
    }
     
  2. cumesoftware

    Senior Member

    Apr 27, 2007
    1,330
    10
    This would normally happen if the input variable is of type int. In this case you are reading a number as a value, not as a character, despite of having a character string. If you want to read as a character or as a character string (opens the possibility of a multiple characters input), you should use cin.getline (). Remember that getline () always attaches a "\n" character at the end.
     
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    To add to what cumesoft said, after the user inputs a string of characters, you will need to convert the string into a number that you can use in your switch statement. One function you can use to do this is atoi(), declared in stdlib.h.

    Here's how you would use it:
    Code ( (Unknown Language)):
    1.  
    2. #include <stdlib.h>
    3. // other #includes
    4.  
    5.  
    6. int main()
    7. {  
    8.   char str[] = "12";
    9.   int num = atoi(str);
    10.   cout << "Converted value is " << num;
    11. }
    12.  
    The code above is probably OK, but I didn't compile it, so caveat emptor. If the string argument to atoi() can't be converted to an int, the function returns 0.

    atoi() and the other string conversion functions (atof, atod, atol) are part of the older C standard library. I checked to see if there were any newer C++-style functions that did the same thing, but I couldn't find any (which doesn't mean they don't exist). In any case, this should do the trick for you.
    Mark
     
Loading...