c++ Case help

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

  1. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    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;
    }
     
  2. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    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;
    }
     
  3. Colin Mac

    Member

    Mar 11, 2008
    18
    0
    make2 would have to be a string, if you want it to hold "seat".
    A char only holds one character.
     
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Why do you have two switch statements?
    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
     
  5. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    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
     
  6. silvrstring

    Active Member

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

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    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
     
  8. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    stevy123,
    Here's one option.

    Code ( (Unknown Language)):
    1.  
    2. #include <string.h>
    3.  
    Declare an array of type char that's big enough to hold the longest car name.
    Code ( (Unknown Language)):
    1.  
    2. char Make2[12];
    3.  
    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,
    Code ( (Unknown Language)):
    1.  
    2. strcpy(Make2, "SEAT");
    3.  
    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:
    Code ( (Unknown Language)):
    1.  
    2. strcpy_s(Make2, "SEAT");
    3.  
    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:
    Code ( (Unknown Language)):
    1.  
    2. if (strcmp(Make1, Make2)
    3. {
    4.     // do something if they are the same
    5. }
    6. else
    7. {
    8.     // do something else if they're different
    9. }
    10.  
    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
    Code ( (Unknown Language)):
    1. string Make2
    2.  
    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
     
  9. stevy123

    Thread Starter Active Member

    Nov 19, 2007
    61
    0
    thank you very much for your help mark. Put that into my code and is working.

    Thank you steve
     
Loading...