Mc Ocelot
Mc Ocelot

Reputation: 1

Switch Case always goes to default

I am trying to make a small operating system that takes a response from a switch...case to go to a miniature game or a simple calculator. However, no matter what input I give (even correct ones) the output is always the default. The compiler I am using (Microsoft Visual Studio; It could be the problem) isn't giving me any errors, and I can't find or think of any mistakes. Do some of you people who are actually good at this have any answers to my problem?

#include "stdafx.h"
#include <iostream>
#include <limits>

using namespace std;

int calc() {

char op;
float num1, num2;

cout << "Enter operation:";
cin >> op;

cout << "Enter two numbers:";
cin >> num1 >> num2;

switch (op)
{
case '+':
    cout << num1 + num2;
    break;

case '-':
    cout << num1 - num2;
    break;

case '*':
    cout << num1 * num2;
    break;

case '/':
    cout << num1 / num2;
    break;

default:
    cout << "That is not an operation";
    break;
}

return 0;
};

int main()
{
char answer;

cout << "Welcome to the FR Operating System. \n";
cout << "If you want to go to the calculator, type in 'Calc'. \n";
cout << "If you want to go to the game, type in 'Game'. \n";

cin >> answer;

switch (answer) {
case 'Calc' || 'calc':
    cout << "Welcome to the calculator. \n";
    break;

case 'Game':
    cout << "Welcome to our game, 'A Day in the Life'. \n";
    break;

default:
    cout << "That is an invalid answer. This has caused the system to crash. \n";
    break;
}

atexit([] { system("PAUSE"); });

return 0;

}

Upvotes: 0

Views: 1497

Answers (3)

sebrockm
sebrockm

Reputation: 6002

  1. 'Game' is not a valid string
  2. Even if you replace it by "Game", which is a valid string, switch doesn't work with strings.

So either use single chars in your switch or use if-else blocks where you compare std::strings via ==.

std::string answer;
cin >> answer;
if (answer == "Calc" || answer == "calc")
    //...
else if (answer == "Game")
    //...
else
    // invalid 

Upvotes: 3

Incomputable
Incomputable

Reputation: 2208

Use map to item callbacks

Ideally, it would be better to map item menu to it's respective actions. std::map<std::string, std::function<void()>> allows exactly that! Read the inline comments to make sense of the rest:

#include <string>
#include <map>
#include <iostream>
#include <functional>

int main()
{
    std::map<std::string, std::function<void()>> menu_items;
    menu_items.emplace("calc", [](){std::cout << "calculate chosen\n";}); //use lambdas to spare boilerplate
    menu_items.emplace("game", [](){std::cout << "game is chosen\n";});

    std::string chosen_item;
    std::cin >> chosen_item;
    auto item = menu_items.find(chosen_item); //search by the string
    if (item == menu_items.end()) //item was not found in the list
        std::cout << "invalid item is chosen\n";
    else
        item->second(); //execute the stored function
}

Demo.

Depending on your usage you might want to use void*() for std::function<void()>, and std::unordered_map for std::map. For your usage case it doesn't seem to matter though.

Also you might want to normalize the input, e.g. lowercase the string, or perform some other normalization. Since this is not performance sensitive part of the code, I believe overhead of std::function and std::map won't matter in this case.

Upvotes: 2

Mohammad Kanan
Mohammad Kanan

Reputation: 4582

You are prompting user for a string while your variable answer is a char, change your prompts to characters like c and g thus make it more convenient, thus you can use and enumerate characters in your switch / case statement:

int main()
{
char answer;

cout << "Welcome to the FR Operating System. \n";
cout << "If you want to go to the calculator, type in 'c'. \n";
cout << "If you want to go to the game, type in 'g'. \n";
cin >> answer;
switch (answer) {
case 'c':
case 'C':
    cout << "Welcome to the calculator. \n";
    break;

case 'g':
case 'G':
    cout << "Welcome to our game, 'A Day in the Life'. \n";
    break;
...

Upvotes: 1

Related Questions