C++ Class Help

Discussion in 'Programmer's Corner' started by stevy123, Mar 14, 2008.

  1. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    Hi All

    I am trying to write a class at the minute for a car insurance program (not to be used for insurance but as an assignment)

    I have included a bit of my code below.

    What i am looking to do is for example when in car type Seat is entered or BMW is entered it gets stored into make but when anything else is entered a message comes up car currently not insured.

    I was looking at doing this with if statements or else the case commands but keep gettin errors. Can anyone tell me how to do this.

    The simple code is below.

    #include <fstream.h>
    #include <iostream>


    class Car_Details
    {
    private:
    char Make [10];

    public:
    void CarData (void)
    {
    cout <<"\nPlease enter Car Make: ";
    cin >> Make;
    }

    void Show_Car(void)
    {
    cout <<"\nCar Make is: " <<Make;
    cout <<endl;
    }

    };


    int main()
    {
    char NewCar; // To enter another Cars Details
    Car_Details Car; // Creating an Object of Car in typr Car_details
    fstream carinfo; //Creating a file to save retreieve data from

    carinfo.open("cardetails.dat", ios::trunc | ios::eek:ut | ios::in); //trunc overwrites the info previously inputted

    do
    {
    cout << "\nEnter Car details";
    Car.CarData();
    carinfo.write( (char*)&Car, sizeof(Car) ); //Write to finaldata
    cout << "\nDo you want to enter another Cars Details? (Yes or No?) ";
    cin >> NewCar;
    }
    while (NewCar == 'yes' || NewCar == 'YES' || NewCar == 'Yes' ||NewCar == 'Y' || NewCar == 'y'); //enter a new client if question is answered to yes

    carinfo.seekg(0);
    carinfo.read( (char*)&Car, sizeof(Car) );
    while (!carinfo.eof()) //Quit on End of File
    {
    cout << "\nCar Details";
    Car.Show_Car();
    carinfo.read( (char*)&Car, sizeof(Car) ); //Read from file

    }
    return(0);

    }

    Any help greatly appricated
     
  2. cumesoftware

    Senior Member

    Apr 27, 2007
    1,330
    10
    Lets take a look at your code:
    Code ( (Unknown Language)):
    1. class Car_Details
    2. {
    3.     private:
    4.     char Make [10];
    5. ...
    Since the variable Make has space for ten chars, anything longer than that (including the termination) will not fit into the C string. You can declare C strings dynamically, or you can use C++ strings (for the last, you need to include string.

    So, my guess us that the other makes are not included because they are too long. I advise you to use a chomp () function in your main file to take the returns that might exist at the end of the string.
     
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I see another problem. You have a variable, NewCar, which is declared in main this way:
    Code ( (Unknown Language)):
    1. char NewCar; // To enter another Cars Details
    2.  
    NewCar is used later in main in this way:
    Code ( (Unknown Language)):
    1. while (NewCar == 'yes' || NewCar == 'YES' || NewCar == 'Yes' ||NewCar == 'Y' || NewCar == 'y');
    2.  
    Because you have declared NewCar as a char variable, it can hold a single character. In your while condition, you are comparing NewCar to several values that aren't type char; namely 'yes', 'YES', and 'Yes'. These are string literals, not character literals, so I would think that the compiler would flag this as an error. It won't have any problem with 'Y' or 'y', which are valid character constants.

    From your while condition, your intent seems to be to let the user enter several variants of yes, many of which are string literals. You can't compare string literals using the == operator in C++, at least not in the way you want. The value of a string constant doesn't have anything to do with the characters that make up the string. Instead, the value of a string constant is its address in memory.

    You mentioned that if you enter anything for car make other than Seat (SEAT - the Spanish version of the Fiat?) or BMW, you get a message saying the car is not insured. Cumesoft has pointed out that your variable for car make can store only 10 characters, including the terminating null. When you entered other car makes, did you try any with nine or fewer characters? If so, the problem is not with the amount of storage for the car's make, but somewhere else.

    Also, I don't see anywhere in your code that will generate a message saying the car isn't insured.

    Finally, you said that you were having problems with the logic using if and switch statements. For one thing, C++ doesn't allow you to use cases that switch on the value of a string constant. C# does, but that's no help if you have to write this in C++. If you're trying to use if statements where you're comparing string contants, that's not going to work, either, for a reason alluded to earlier. For example, this statement:
    Code ( (Unknown Language)):
    1. if ("cat" < "dog")
    2. {
    3.    ...
    4. }
    might turn out true or it might turn out false, either way depending on where the string literals are stored in memory.

    If you need to compare the values of strings, you can use standard library C functions such as strcmp(), or String.Compare in the String class of the System namespace, if you're using a fairly recent Microsoft compiler. If you're using another compiler, you'll need to find out what you have available to work with.

    Mark
     
  4. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    thanks guys for your replys.

    Yes i know i have not included any code that will choose if the car is insured or not as i dont know what to write.

    If i am not to use Char then what should i use? Im only a begineer programming in C++ and not very sure of it as yet.

    If anyone could show me how to make my car make accept Seat and Bmw but reject anything else and terminate the program this would be very much appricated as i cant seem to figure it out.

    Regards
    Steve
     
  5. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    Can anyone point me in the right direction to solving this problem please. Im really stuck with it and tried alot of things.

    If i should not use char for Make what should i declare it as?

    Regards
    steve
     
  6. Colin Mac

    Member

    Mar 11, 2008
    18
    0
    Declare make as type string and read using getline().
    You can declare newCar as char just don't prompt the user to enter a string.
    Change the (yes/no) to (y/n).
     
  7. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    Thanks for the reply,

    I have changed the yes statements to just y and n and got that part working.

    So can anyone help me on how to make my if chain?

    Ive tried a few ways but keep getting errors.

    Looking it to accept Seat and bmw and reject anything else and end program.

    Any ideas?
     
  8. cumesoftware

    Senior Member

    Apr 27, 2007
    1,330
    10
    Have you tried to rebuild/remake the whole project (if your compiler has the option)?
     
  9. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    yes i have rebuilt the program and what i have is working.

    But am looking to create an if chain if 'Seat' or BMW is entered the program accepts this but if anything else is entered it does not allow and ends the program.

    Can anyone tell me how to do this?

    Thanks
    steve
     
  10. Colin Mac

    Member

    Mar 11, 2008
    18
    0
    You can compare strings like this

    Code ( (Unknown Language)):
    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4.  
    5. int main ()
    6. {
    7.   string str1 ("skoda");
    8.   string str2 ("seat");
    9.  
    10.   if (str1.compare(str2) == 0)
    11.      cout << "Strings are equal";
    12.  
    13.   if (str1.compare(str2) != 0)
    14.      cout << "Strings are not equal";
    15.  
    16.   cin.get();
    17.   return 0;
    18.  
    19. }
     
  11. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    Hi thanks for that,

    is it possible to put in a fuction that if it seat or skoda or bwn are entered that it will see it as equal? but if anything else it will not be equal? Allowing user input?

    seat || Skoda ||Bmw

    cheers
    steve
     
  12. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Let me rephrase your question to see if I understand what you're asking: Is it possible to write a function that returns true if SEAT, Skoda, or BMW are entered, and false otherwise?

    If that's the question, then yes.

    One way to do it would be to write a function that takes a char[] parameter (i.e., an array of characters). Inside the body of the function use a sequence of if statements, each one using strcmp to compare the passed-in string with one of the three allowed string constants. As soon as one of the if statements compares successfully with one of the three allowed string constants, return true (or return 1).

    The function would look something like this.
    Code ( (Unknown Language)):
    1.  
    2. int compare_test(char [] str)
    3. {
    4.    if (strcmp(str, "bmw"))
    5.       return 1;
    6.    if (strcmp(str, "seat"))
    7.       return 1;
    8.    if (strcmp(str, "skoda"))
    9.       return 1;
    10.    return 0;
    11. }
    12.  
    Note that you might want to convert the user's input string to lowercase. With this in mind, I used lowercase forms of the cars' names. The standard library function tolower() could be used, but it works on only a single character, so to use this, you might need to convert each letter in the car's name (e.g., BMW). There's probably another function that could convert all letters in a string to lower case, but I don't have time right now to look for it.

    Hope this helps.
    Mark
     
  13. cumesoftware

    Senior Member

    Apr 27, 2007
    1,330
    10
    I don't know, but I'm guessing that your program still doesn't handle very well the long character strings. It should accept anything by now.
     
Loading...