c++ Case help

Thread Starter

stevy123

Joined Nov 19, 2007
61
Hi All I am trying to write a simple program using cases to select the option.

Below i have copied some of the program in.
Basically what i am looking to do. When you enter 1 it selects seat and prints out that seat has been selected. What i am trying to do now is to make a variable called Make1 and store seat in it. Then make another variable in the private details called make 2.
When i call the show car data at the bottom i want it to display seat but am having problems doing so.

The code i have pasted below is not the full program just some of it copied on here.

Can anyone please tell me how to do this?

Thanks
Steve


class Car_Details //Name of class is Car_Details
{
private:
int Make;
public:
void CarData (void)
{
cout << "\nPlease Select Make of Car: i.e Enter 1 for Seat\n"
<< " 1. Seat:\n"
<< " 2. Bmw :\n"
<< " 3. Volkswagen:\n"
<< " 4. Ford:\n"
<< " 5. Mercedes:\n"
<<" 6. Renault:\n";
cin >> Make;

switch( Make)
{

case 1:
cout << "Car Selected is a Seat\n";
{
Price1 = 40;
char Make1= "seat";
Make2 = Make1;

}
break;
switch( Make )
{

case 2:
cout << "Car Selected is a Bmw\n";
{
Price1 = 100;
}
break;

void Show_Car(void)
{

cout <<"\nMake is: "<<Make2;
cout <<endl;
}
 

Thread Starter

stevy123

Joined Nov 19, 2007
61
I Have got somewere with trying my program and can get it to output 1 character as shown below. How can i get Make 2 to display the full name such as seat?

Any ideas??


class Car_Details //Name of class is Car_Details
{
private:
int Make;
Char Make2;
public:
void CarData (void)
{
cout << "\nPlease Select Make of Car: i.e Enter 1 for Seat\n"
<< " 1. Seat:\n"
<< " 2. Bmw :\n"
<< " 3. Volkswagen:\n"
<< " 4. Ford:\n"
<< " 5. Mercedes:\n"
<<" 6. Renault:\n";
cin >> Make;

switch( Make)
{

case 1:
cout << "Car Selected is a Seat\n";
{
Price1 = 40;
Make2 ='s';

}
break;
switch( Make )
{

case 2:
cout << "Car Selected is a Bmw\n";
{
Price1 = 100;
}
break;

void Show_Car(void)
{

cout <<"\nMake is: "<<Make2;
cout <<endl;
}
 

Mark44

Joined Nov 26, 2007
628
Why do you have two switch statements?
switch( Make)
{

case 1:
cout << "Car Selected is a Seat\n";
{
Price1 = 40;
Make2 ='s';

}
break;
switch( Make )
{

case 2:
cout << "Car Selected is a Bmw\n";
{
Price1 = 100;
}
break;
If I understand what you're trying to do, you should have only one switch statement and it should have seven cases (six for the six kinds of cars you list and a default case for when the user enters something other than 1 through 6).

As Colin Mac points out, Make2 is type char, so it can hold only a single character. If you want Make2 to be able to hold anything more than a single character, it will need to be a string type or an array of char.

One other thing: Is there some reason you posted two copies of your code? If there's any difference between the code in your first post in this thread and the second, I don't see it.

Mark
 

Thread Starter

stevy123

Joined Nov 19, 2007
61
Hi Thanks for your replies

What way do i go about making a string then? what is the command to make it be a string?

is it string Make2?

The second one is slightly different.

Yes i see that now that i should not have the second switch statement but i only coped and pasted some of the program in.

Thanks
steve
 

silvrstring

Joined Mar 27, 2008
159
stevy123: You need to include the string header file (#include<string>). Then you need to change your variable data types from char to string.
 

Thread Starter

stevy123

Joined Nov 19, 2007
61
thanks for that reply,

i have included the #include <string> but when i go to define my variable Make 1 as string it comes up with the error

error C2146: syntax error : missing ';' before identifier 'Make2'

I tried to define it as
string Make2

is that wrong?

regards
steve
 

Mark44

Joined Nov 26, 2007
628
stevy123,
Here's one option.

Rich (BB code):
#include <string.h>
Declare an array of type char that's big enough to hold the longest car name.
Rich (BB code):
char Make2[12];
Now you have a variable that can hold a string of characters. You can't just assign a string constant to it; you need to use a function that will do this. For example,
Rich (BB code):
strcpy(Make2, "SEAT");
This is a standard ilbrary function that is declared in string.h. My compiler gives me a warning that this is an unsecure function, and recommends that I use strcpy_s. The line of code above would then look like:
Rich (BB code):
strcpy_s(Make2, "SEAT");
To compare two strings, you can use strcmp, which takes two string arguments. If the strings are the same, it returns 0. So for example, if you have two string variables, with one holding "SEAT" and the other "BMW", you can compare them like this:
Rich (BB code):
if (strcmp(Make1, Make2)
{
    // do something if they are the same
}
else
{
    // do something else if they're different
}
That should get you going. You should be aware that my advice has been geared more toward C-style programming, rather than C++ style. My knowledge about the C++ way of dealing with strings is less complete, since I haven't been paying as much attention to C++ for a while.

In response to a couple of your questions, you have
Rich (BB code):
string Make2
That's an error because you don't have a semicolon at the end. Every C/C++ statement ends with a semicolon. Also, assuming there was a type named string, what you are doing is declaring Make2, not defining it. There's a difference: when you declare something, you are informing the compiler about some sort of variable or function. When you define something, the compiler allocates memory for the thing, and may or may not store a value in that memory. You can declare something more than once, but you can define it only once.
Good luck!
Mark
 
Top