-1

I am new to C++ and programming in general and was trying to figure out a way to create a switch in C++ to trigger when a number entered is divisible by 3, by 5, and by both 3 and 5. Here is what I have so far:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int number;

    cout << "Please input a number and then press the enter key" << endl;
    cin >> number;

    switch (number){
    case "/3":
        cout << "Fizz" << endl;
        break;
    case "/5":
        cout << "Buzz" << endl;
        break;
    case "/3" "/5":
        cout << "FizzBuzz" << endl;
        break;
    default:
        cout << "Please select another number." << endl;
    }

}

Any help with this would be greatly appreciated! :)

Kelos
  • 19
  • 3
    You need to read some basic C/C++ book. `switch` is only able to check for a perfect match. – HolyBlackCat Jun 14 '16 at 14:53
  • 2
    Be nice. Check out this link, it may help: http://www.cprogramming.com/tutorial/modulus.html – Script Kitty Jun 14 '16 at 14:54
  • Well, first up, you cannot switch on a string. – Sean Jun 14 '16 at 14:57
  • Secondly, you're not actually checking if they are divisible. Why would you think that "/3" would be a check? – Sean Jun 14 '16 at 14:58
  • @Sean " you cannot switch on a string " - you can, if you map the enum element to a string. – Mara Black Jun 14 '16 at 15:59
  • 1
    @M010 - Maybe you'd like to elaborate? – Sean Jun 14 '16 at 16:00
  • #include #include #include using namespace std; enum seasons { Winter , Autumn }; seasons getStringFromEnum(string inputSeason) { map seasonMap; seasonMap["SeasonWinter"] = seasons::Winter; seasonMap["SeasonAutumn"] = seasons::Autumn; return seasonMap[inputSeason]; } void main(){ std::string input = "SeasonWinter"; seasons season = getStringFromEnum(input); switch(season){ case seasons::Winter: cout << " Winter case" << endl ; break; case seasons::Autumn: cout << "Autumn case" << endl; break;} } // winter case – Mara Black Jun 14 '16 at 16:27
  • http://pastebin.com/db67wzhx – Mara Black Jun 14 '16 at 16:27
  • @M010 - right, so like I said, you cannot switch on a string. – Sean Jun 14 '16 at 16:41

3 Answers3

9

In C++ the switch labels must be compile-time evaluable constant expressions that are integral types.

"/3", for example, is a string literal and so does not fit that requirement.

In this case, use number % 3 == 0 to test for divisibility by 3, and so on and use an if, else block:

if (number % 15 == 0){
    /*FizzBuzz - do this one first as my `if` block is not mutually exclusive*/
} else if (number % 3 == 0){
    /*Fizz*/
} else if (number % 5 == 0){
    /*Buzz*/
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thank you! I know it was an amateur question, and I was between using if statements and a switch. Visual Studio kept giving an error saying that my initial expression needed to be a constant. In any case thank you so much again! :) – Kelos Jun 14 '16 at 15:27
1

You can use if else.

int remainder1 = 0, remainder2 = 0;
remainder1 = number % 3;
remainder2 = number % 5;

if(remainder1 == 0 && remainder2 ==0) // both
       cout<<"FizzBuzz"<<'\n';
else if(remainder1 == 0)  // number can be divided by 3
       cout<<"Fizz"<<'\n';
else if(remainder2 == 0) // number can be divided by 5
       cout<<"Buzz\n";
else   // neither
       cout<<"......"<<'\n';

BTW, you do have to read the basic book about C++.

here, you can know more about switch

Zhou Yi
  • 55
  • 9
0

If you really want to do with switch, here is a method, but is not nice. The easiest way is how Bathsheba said.

#include <iostream>
#include <cmath>
using namespace std;

enum class divided { DivideBy3  , DivideBy5 , DivideBy3and5 }; // "strong enum" 

// enum class divided { DivideBy3  , DivideBy5 , DivideBy3and5 }; //also good but can be unsafe
divided getCase(int number)
{
  divided div;

  if(number%3 == 0)
        div = divided::DivideBy3;
  if(number%5 == 0)
        div = divided::DivideBy5;
  if(number%3 ==0 && number%5 == 0)
        div = divided::DivideBy3and5;

  return div;
}

int main()
{
  int numberIn;

  cout << "Please input a number and then press the enter key" << endl;
  cin >> numberIn;

  divided number =  getCase(numberIn);

  switch (number)
  {
  case divided::DivideBy3:
      cout << "Fizz" << endl;
    break;
  case divided::DivideBy5:
      cout << "Buzz" << endl;
    break;
  case divided::DivideBy3and5:
      cout << "FizzBuzz" << endl;
    break;
  default:
      cout << "Please select another number." << endl;
   }

}

look at this for enum vs class enum. Keep going.

Community
  • 1
  • 1
Mara Black
  • 1,666
  • 18
  • 23