-
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.
-
-
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();
}
-
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
-
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();
}
-
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...]
}
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:
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.
-
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.
-
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();
}
-
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;
}
-
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.
-
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();
}
-
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
-
Sorry already been suggested multiple times. And problem has been solved.
-
Thank you everyone! That was extremly helpful!