Thread: Multi charater constant

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    Multi charater constant

    I'm new to C++, and I'm currently taking a course and I'm having a tough time with some of the problems. There is a deck of cards problem. problem is asking for a program where the user enters input suchas for example "QD" and "Queen of diamonds" is outputed. Can't seem to get passed an error [ Warning] multi-character character constant. Another problem I'm lost on is dealing with funtions, it just a simple factorial problem.

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    Care to post the code?

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    example of code

    This is just a very quick example of what I think is needed.



    Code:
    int main()
    {
         char card;
         
    cout << "Enter the card notation: " << endl;
    
    cin >> card;
    
    
    if (card = 'QD')
    {
    cout<< " Queen of Diamonds";
    }
    cin.get();
    cin.ignore();
    }

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    card is a char, a char is a single byte while 'QD' is two bytes (a word, a short in C)
    Also card = 'QD' isn't a boolean statement, that tries to set card equl to 'QD', you want card == which tests the value
    For two characters you need a short, not a char

    sorry for the choppiness of this post

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Used your advise, this is the current code. Still running into that the same error


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
         short card;
         
    cout << "Enter the card notation: " << endl;
    
    cin >> card;
    
    
    if (card == 'QD')
    {
    cout<< " Queen of Diamonds";
    
    
    
    }
    cin.get();
    cin.ignore();
    }

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    You can use a string and compare. if (card == "QD").
    But card has to be of a string type.

    If it's ok then you could use two chars.
    Code:
    char value, suit;
    
    cout << "Enter card notation ([value] [suit]): ";
    cin >> value >> suit;
    
    if ((value == 'Q') && (suit == 'D')) 
    { 
       cout << "Queen of Diamonds";
    }
    Alternatively you could use a function so that you dont have to type out each of the 52 cards in the deck.

    Code:
    void printValue (char value) {
       if (value == 'A') cout << "Ace";
       else if (value == '2') cout << "Two";
       else if (value == '3') cout << "Three";
       else if (value == '4') cout << "Four";
       else if (value == '5') cout << "Five";
       else if (value == '6') cout << "Six";
       else if (value == '7') cout << "Seven";
       else if (value == '8') cout << "Eight";
       else if (value == '9') cout << "Nine";
       // [etc...]
    }
    Code:
    'QD'
    Is invalid because as it says multiple character constants.
    A char is represented by ' ', a char is one character not two. To use two you need to have QD be a string like so:
    Code:
    "QD"
    Using the example I gave above, you could substitute a string for it instead of a char:
    Code:
    void printValue (string value) {
       if (value == "A") cout << "Ace";
       else if (value == "2") cout << "Two";
       else if (value == "3") cout << "Three";
       else if (value == "4") cout << "Four";
       else if (value == "5") cout << "Five";
       else if (value == "6") cout << "Six";
       else if (value == "7") cout << "Seven";
       else if (value == "8") cout << "Eight";
       else if (value == "9") cout << "Nine";
       // [etc...]
    }
    Then you would need to split up the string and find the value and suit.
    Last edited by JoshR; 07-18-2005 at 02:21 PM.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    there are some ways to do what you want:
    This uses C++ strings and is probably the easiest way.
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string card;
        std::getline(std::cin, card);
        if(card == "QD")
            std::cout << "Queen of diamonds" << std::endl;
    }
    If you dont want that you can use C-style strings, arrays that is.
    Code:
    #include <iostream>
    #include <cstring>
    
    int main()
    {
        char card[10];
        std::cin.getline(card, sizeof(card));
        if(std::strcmp(card, "QD") == 0)
            std::cout << "Queen of diamonds" << std::endl;
    }
    I havent tested this code, I wrote it directly in the browser so there might be some small errors.

    You can also replace the call to std::getline and to std::cin.getline with cin >>
    it all depends on if you want to allow the user to type spaces betwean the letters or not.

    Another thing to note is that in the second example you can replace 10 with any number you want, but with your example the number must be at least 3. It all depends on if the user always will type in 2 letters or if you want to allow more letters to be inputed.

    For more info on this I suggest you look into arrays and strings.
    Last edited by Shakti; 07-18-2005 at 02:23 PM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    That was very helpful!!! Thanks! Now I just have to deal with the factorial problem. Having a number of error pop up with this one. I'm using Dev C++ for the compiler


    Code:
    #include <iostream>
    #include <cmath>
    int factorial(int num)
    {
     if (num==1)
      return num;
     return factorial(num-1)*num; 
    }
    int main();
    {
        int num;
    cout << "What is the number";
    cin >> num;
    cout << factorial(num-1);
    
    }
    cin.get();
    cin.ingore();
    }

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    I know you already figured it out, but I did something similar that may interest you. This shows you what you can do once you learn how to overload the operators and have it print out your own data types:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    enum suitType {hearts, diamonds, clubs, spades, badsuit};
    enum valueType {two, three, four, five, six, seven, eight, 
                    nine, ten, jack, queen, king, ace, badvalue};
                    
    ostream& operator<< (ostream &Out, valueType value);
    ostream& operator<< (ostream &Out, suitType suit);
    istream& operator>> (istream &In, valueType &value);
    istream& operator>> (istream &In, suitType &suit);
    
    main()
    {
       valueType myvalue;   
       suitType mysuit;
    
       for (int a = 0; a < 13; a++)
       {
          cout << "Enter the card value ---> ";
          cin >> myvalue;
          cout << "Enter the card suit ---> ";
          cin >> mysuit;
          
          cout << endl << myvalue << " of " << mysuit << endl << endl;
       }
       
       cin.get();
       return 0;
    }
    
    ostream& operator<< (ostream &Out, valueType value)
    {
       switch (value)
       {
          case two: Out << "two"; break;
          case three: Out << "three"; break;
          case four: Out << "four"; break;
          case five: Out << "five"; break;
          case six: Out << "six"; break;
          case seven: Out << "seven"; break;
          case eight: Out << "eight"; break;
          case nine: Out << "nine"; break;
          case ten: Out << "ten"; break;
          case jack: Out << "jack"; break;
          case queen: Out << "queen"; break;
          case king: Out << "king"; break;
          case ace: Out << "ace"; break;
          case badvalue: Out << "invalid value"; break;
       }
       return Out;
    }
    
    ostream& operator<< (ostream &Out, suitType suit)
    {
       switch (suit)
       {
          case hearts: Out << "hearts"; break;
          case diamonds: Out << "diamonds"; break;
          case clubs: Out << "clubs"; break;
          case spades: Out << "spades"; break;
          case badsuit: Out << "invalid suit"; break;
       }
       return Out;
    }
    
    istream& operator>> (istream &In, valueType &value)
    {
       string temp;
       In >> temp;
       
       for (int count = 0; count < temp.size(); count++)
          if ('A' <= temp[count] && temp[count] <= 'Z')
             temp[count] += 32;
             
       if (temp == "two") value = two;
       else if (temp == "three") value = three;
       else if (temp == "four") value = four;
       else if (temp == "five") value = five;
       else if (temp == "six") value = six;
       else if (temp == "seven") value = seven;
       else if (temp == "eight") value = eight;
       else if (temp == "nine") value = nine;
       else if (temp == "ten") value = ten;
       else if (temp == "jack") value = jack;
       else if (temp == "queen") value = queen;
       else if (temp == "king") value = king;
       else if (temp == "ace") value = ace;
       else value = badvalue;
       
       return In;
    }
    
    istream& operator>> (istream &In, suitType &suit)
    {
       string temp;
       In >> temp;
       
       for (int count = 0; count < temp.size(); count++)
          if ('A' <= temp[count] && temp[count] <= 'Z')
             temp[count] += 32;
             
       if (temp == "hearts") suit = hearts;
       else if (temp == "diamonds") suit = diamonds;
       else if (temp == "clubs") suit = clubs;
       else if (temp == "spades") suit = spades;
       else suit = badsuit;
       
       return In;
    }

  10. #10
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    out of curiosity, what compiler are you using? I can't reproduce that warning even with warning level 4 in C or C++, I am using cl.

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    #include <iostream>
    // dont need cmath header
    using namespace std; //forgot to declare that your using std
    
    int factorial(int num)
    {
     if (num==1)
      return num;
     return factorial(num-1)*num; 
    }
    int main()  // had a semicolon here: error
    {
        int num;
    cout << "What is the number";
    cin >> num;
    cout << factorial(num-1);
    
    //bracket here...main doesnt finish yet ;)
    //First clear the stream then wait for input
    cin.ignore(); // spelled ignore wrong :-P
    cin.get();
    
    }
    Last edited by JoshR; 07-18-2005 at 02:40 PM.

  12. #12
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Or you could use an array of chars, or a string
    Code:
    char card[2];  // Will now store 2 characters
    //Or 
    string card; // will store beyond 2 characters, but you need to include the string library at the top
    #include <string> //needed for strings

  13. #13
    *this
    Join Date
    Mar 2005
    Posts
    498
    Sorry already been suggested multiple times. And problem has been solved.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Thank you everyone! That was extremly helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM