-1

How to make this work

 #include <stdio.h>
    #include <conio.h>
    main()
    {
        char C;
        printf("R=This Program gives you some of the information of the country you entered.");
        printf("Enter a Country:");
        scanf("%c", &C);
        switch(C)
        {
            case 'Algeria':
                printf("Capital: Algiers");
                printf("Currency and Country Code: Algerian Dinar (DZD)");
                break;
            }   



        getch ();
        return 0;
    }

The Error is

  • 11 8 C:\Users\Edrian\Desktop\C++ Codes\World Information.cpp [Warning] character constant too long for its type

  • C:\Users\Edrian\Desktop\C++ Codes\World Information.cpp In function 'int main()':

  • 11 8 C:\Users\Edrian\Desktop\C++ Codes\World Information.cpp [Warning] case label value exceeds maximum value for
    type

Technoed
  • 17
  • 8
  • 1
    Related: [Why the switch statement cannot be applied on strings?](https://stackoverflow.com/questions/650162/why-the-switch-statement-cannot-be-applied-on-strings) – Yksisarvinen Jan 27 '19 at 12:26
  • 1
    Reminder: a `char` or *character* is a single, one, letter or symbol. A *string* is zero or more characters. The `switch` works with *single* characters. – Thomas Matthews Jan 27 '19 at 17:57

3 Answers3

1

Char contains only one character if you want to use more than one character you have to use string data type.In your case you should use string

Arain001
  • 109
  • 1
  • 2
  • 10
1

11 8 C:\Users\Edrian\Desktop\C++ Codes\World Information.cpp [Warning] character constant too long for its type

This says that 'Algeria' is too long for the type char, which should be a single letter, e.g. 'A' (as suggested here).

    char string[256];
    scanf("%255s", string);
    switch (s[0]) {
        case 'A':
            if (0 == strcmp(string, "Algeria")) {

This should work. You may need to #include <string.h> for strcmp.

Notice how "Algeria" is now double-quoted, while 'A' is single-quoted.

I elided out lines that would be the same in both, mostly printf statements. Make sure that you get the curly braces right. I added a { for the if. You'll need to add a } to match (before the break;).

You could also say

if (!strcmp(string, "Algeria")) {

But I thought that it might be easier for you to read the other way. Functionally the two statements are the same, as C considers zero to be falsey.

See also:

mdfst13
  • 850
  • 8
  • 18
0

Know the difference :

'Algeria' this is a string literal so it should be "Algeria" and your C should be a string

'A' is a character

Spinkoo
  • 2,080
  • 1
  • 7
  • 23