0

When I run the line of code it always goes to the default case, can anybody explain to me where I could have gone wrong?

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    char day;
    cout << "enter what day it is" << endl;
    cin >> day;
    switch (day) {
        case 'monday':
            cout << day << "you have 6 classes " << endl;
            break;

        case 'friday':
            cout << day << "you have 5 classes" << endl;
            break;

        default:
            cout << "you dont have classes " << endl;
            break;
    }
    system ("PAUSE");
    return 0;
}
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
  • 8
    `char` type is only enough to store one letter and [there is a difference between single quotes and double quotes](https://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c-or-c) and [you cannot use strings in `switch` statements](https://stackoverflow.com/questions/650162/why-the-switch-statement-cannot-be-applied-on-strings). – Yksisarvinen Mar 02 '20 at 21:39
  • 1
    Single quotes don't make strings – JVApen Mar 02 '20 at 21:39
  • how do i make it store more than 1 characther in char? whats another syntax i can possibly use? – manlikething1233 Mar 02 '20 at 21:42
  • 1
    Your compiler may be warning you about `'monday'` and `'friday'`. If it isn't consult the compiler documentation to see if you can turn up the warning level. Warnings are the first line of defense against logic errors. If the compiler can catch them for you, you can save a lot of time debugging. That's assuming you can understand the diagnostic message, but that comes with time and practice. – user4581301 Mar 02 '20 at 21:44
  • You cannot use string in `switch`. Single `char` would be okay (because it can be converted to `int`). If you want user to enter full string like `monday`, you need to rethink your approach (you can use series of `if` statements, `std::map` or a couple of other approaches). – Yksisarvinen Mar 02 '20 at 21:45
  • a `char` is exactly enough space for a single character (with a few weird exceptions that aren't worth going into here like the 32 bit `char`s on old Cray computers) . You cannot put two characters in one `char`, but you can map a string to something that is `switch`-able. – user4581301 Mar 02 '20 at 21:46
  • thank you @Yksisarvinen – manlikething1233 Mar 02 '20 at 21:47
  • std::string has an overload for the == operator. This means if you say std::string day, and then say, instead of switch, if (day == "monday") (double quotes important) then that should work. – FShrike Mar 02 '20 at 21:53
  • @Yksisarvinen - Any built-in integral type can be used in a `switch` statement. `char` is an integral type. No need for conversion to `int` to use it in a `switch` statement. – Peter Mar 03 '20 at 02:41

2 Answers2

1

You'll need to use an if-else-if ladder or use a table.

if-else-if ladder:

if (text == "apple")
{
    Eat_Apple();
}
else if (text == "steak")
{
    Eat_Steak();
}
else
{
    Starve();
}

You may want to transform the string into all lower case or all upper case before comparing.

Maps of function pointers
An alternative is to use std::map with function pointers or function objects.

typedef void (*Function_Pointer)(); // Synonym for function pointer.
std::map<std::string, Function_Pointer> database;
//...
database["apple"] = Eat_An_Apple;
database["steak"] = Eat_A_Steak;

std::map<std::string, Function_Pointer>::iterator iter = database.find(text);
if (iter != database.end())
{
    (*iter)(); // Execute via the function pointer.
}
else
{
    Starve();
}

The Table
A third alternative is to use a table of pairs, where the value is the function pointer.

struct Entry
{
    std::string name;
    Function_Pointer function;
};
static const Entry database[] =
{
    {"apple", Eat_An_Apple},
    {"steak", Eat_A_Steak},
};
static const unsigned int table_quantity =
    sizeof(database) / sizeof(database[0]);

A nice attribute of the table is that it is constant data and initialized before the program starts. Technically, most compilers throw it into the data section or reference it in the data section. No time wasted initializing an ADT.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

You can't pass string as a switch quantity, switch quantity should be an integer.

Try it using enum:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

enum week {Mon, Tue, Wed, Thu, Fri, Sat, Sun};

week StrToDay(const string& str)
{
    if(str=="Monday") return Mon;
    if(str=="Tuesday") return Tue;
    if(str=="Wednesday") return Wed;
    if(str=="Thursday") return Thu;
    if(str=="Friday") return Fri;
    if(str=="Saturday") return Sat;
    if(str=="Sunday") return Sun;
}

int main() {
    week day;
    std::string dd;
    cout << "enter what day it is " << endl;
    getline(cin, dd);
    day=StrToDay(dd);

    switch (day) {
        case Mon:
            cout << dd << " you have 6 classes " << endl;
            break;

        case Fri:
            cout << dd << " you have 5 classes" << endl;
            break;

        default:
            cout << "you dont have classes " << endl;
            break;
    }
    return 0;
}