C++ Class Help

Thread Starter

stevy123

Joined Nov 19, 2007
61
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
 

bloguetronica

Joined Apr 27, 2007
1,541
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.
...
Lets take a look at your code:
Rich (BB code):
class Car_Details
{
	private:
	char Make [10];
...
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.
 

Mark44

Joined Nov 26, 2007
628
I see another problem. You have a variable, NewCar, which is declared in main this way:
Rich (BB code):
char NewCar; // To enter another Cars Details
NewCar is used later in main in this way:
Rich (BB code):
while (NewCar == 'yes' || NewCar == 'YES' || NewCar == 'Yes' ||NewCar == 'Y' || NewCar == 'y');
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:
Rich (BB code):
if ("cat" < "dog")
{
   ...
}
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
 

Thread Starter

stevy123

Joined Nov 19, 2007
61
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
 

Thread Starter

stevy123

Joined Nov 19, 2007
61
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
 

Colin Mac

Joined Mar 11, 2008
18
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).
 

Thread Starter

stevy123

Joined Nov 19, 2007
61
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?
 

bloguetronica

Joined Apr 27, 2007
1,541
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?
Have you tried to rebuild/remake the whole project (if your compiler has the option)?
 

Thread Starter

stevy123

Joined Nov 19, 2007
61
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
 

Colin Mac

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

Rich (BB code):
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str1 ("skoda");
  string str2 ("seat");

  if (str1.compare(str2) == 0)
     cout << "Strings are equal";
  
  if (str1.compare(str2) != 0)
     cout << "Strings are not equal";

  cin.get();
  return 0;
 
}
 

Thread Starter

stevy123

Joined Nov 19, 2007
61
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
 

Mark44

Joined Nov 26, 2007
628
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.
Rich (BB code):
int compare_test(char [] str)
{
   if (strcmp(str, "bmw"))
      return 1;
   if (strcmp(str, "seat"))
      return 1;
   if (strcmp(str, "skoda"))
      return 1;
   return 0;
}
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
 

bloguetronica

Joined Apr 27, 2007
1,541
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
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.
 
Top