CoffeeRain
CoffeeRain

Reputation: 4522

Compiler error "character constant too long for its type". What's wrong?

I have some code that I'm trying to work on...

#include <iostream>
#include <string>

int main()
{
  std::cout << "Hello. Welcome to Delicious Drive Up. What would you like to order?\n";
  std::cout << "\nOur menu is-";
  std::cout << "...";
  std::cout << "\nOrder here > ";
  std::string choice;
  std::getline(cin, choice);
  if (choice == 'hamburger' || choice == 'Hamburger')
  {
      std::cout << "We don't have any ham. Is a Chickenburger all right? y/n. > ";
      std::string opt;
      std::getline(cin, opt);
      if (opt == 'y' || opt == 'Y' || opt == 'yes' || opt = 'Yes')
      {
          std::cout << "Here's your chickenburger.";
      }
  }
}

This was adapted from a Bash script I wrote and is one of my first C++ programs. When I compile this, it comes up with these errors...

test.cpp:19:15: warning: character constant too long for its type
test.cpp:19:40: warning: character constant too long for its type
test.cpp:23:44: warning: multi-character character constant
test.cpp:23:59: warning: multi-character character constant
test.cpp: In function ‘int main()’:
test.cpp:19: error: no match for ‘operator==’ in ‘choice == 1919378802’
test.cpp:19: error: no match for ‘operator==’ in ‘choice == 1919378802’
test.cpp:23: error: no match for ‘operator==’ in ‘opt == 'y'’
test.cpp:23: error: no match for ‘operator==’ in ‘opt == 'Y'’
test.cpp:23: error: no match for ‘operator==’ in ‘opt == 7955827’

Could you explain what these mean and how to fix them?

EDIT: I get a new error message now...

.test.cpp: In function ‘int main()’:
.test.cpp:23: error: no match for ‘operator||’ in ‘((std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& opt))), ((const char*)"y")) || std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& opt))), ((const char*)"Y"))) || std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& opt))), ((const char*)"yes"))) || opt’
.test.cpp:23: note: candidates are: operator||(bool, bool) <built-in>

Upvotes: 20

Views: 118933

Answers (3)

Adhil Muhammed
Adhil Muhammed

Reputation: 17

You are using single quotes to enclose your string.

if (choice == 'hamburger' || choice == 'Hamburger')

change it to double quotes.

if (choice == "hamburger" || choice == "Hamburger")

The single quote is used to define single character and double quotes is used to define string literal.

Upvotes: 1

Pietro Lorefice
Pietro Lorefice

Reputation: 1487

You're using single quotes to enclose a string. You need to change

if (choice == 'hamburger' || choice == 'Hamburger')

to

if (choice == "hamburger" || choice == "Hamburger")

The same thing applies to 'Yes' and 'yes', of course.

As for the second problem, you're trying to compare a single character with a string. You need to consider your 'Y' as a string too:

if (opt == "y" || opt == "Y" || opt == "yes" || opt == "Yes")
       //  ^^^ Note the double quotes also on single characters

Upvotes: 5

wolfgang
wolfgang

Reputation: 4953

As others have pointed out, you need to use double quotes ("y" instead of 'y') for your strings, otherwise they are character literals.

In C/C++, there is such a thing as a multi-character literal; its value is a number made up of somehow putting the character codes for the individual characters together in some implementation-defined way. You don't want to ever use them unless you have a really really good reason. They only reason you need to know about them is to understand the warnings and error messages:

test.cpp:19: error: no match for ‘operator==’ in ‘choice == 1919378802’

... means that there is no way to compare a string with the number 1919378802, which is what your compiler interprets 'hamburger' to mean.

Once that is fixed, your new error message:

.test.cpp:23: error: no match for ‘operator||’ in ...
.test.cpp:23: note: candidates are: operator||(bool, bool) <built-in>

means that something went wrong with one of the || operators. Maybe one of its operands wasn't actually a boolean expression. The "note" tells you that there is a built-in || for two bools, but that it couldn't be used in this situation.

Solution: Replace opt = 'Yes' by opt == "Yes".

The single =, assignment, means that the result of that expression is not a bool but a string, and there is no operator|| for or-ing a boolean with a string.

Style Note: It's usually considered better style to not use a using namespace std declaration. Instead, explicitly refer to standard library stuff (cout, endl, string, getline) using a std:: prefix, as in std::string.

Upvotes: 43

Related Questions