4 Replies - 230 Views - Last Post: 04 November 2017 - 01:05 PM Rate Topic: -----

#1 jadecatunao  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-November 17

Switch Statement and Program

Posted 04 November 2017 - 11:26 AM

#include <stdio.h>
int main (void)
{
    int y;
    int n;
    int m;
    switch (y)
    {
        case y: printf ("yes/n");
                break;
        case n: printf ("no/n");
                break;
        case m: printf ("menu/n");
                break;
        default: printf ("exit/n");
                 break;
    }
}



-everytime i run it, it says "'y' cannot appear in a constant expression", and the same for n and m. How do I fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: Switch Statement and Program

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2513
  • View blog
  • Posts: 3,993
  • Joined: 21-June 11

Re: Switch Statement and Program

Posted 04 November 2017 - 11:45 AM

Can you explain what you're trying to do here? Are you trying to check whether the variable is equal to one of the other variables? If so, you can use if-else if instead of a switch.

Or are you trying to check whether the variable y contains one of the characters 'y', 'n' or 'm'? If so, you need to add single quotes around the characters (and you don't need to define the other variables).

In either case your variables need to have values before you can compare them to anything. Using uninitialized variables invokes undefined behavior.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1378
  • View blog
  • Posts: 4,320
  • Joined: 20-March 10

Re: Switch Statement and Program

Posted 04 November 2017 - 11:48 AM

Further y n and m are variables and variables cannot be used in constant (fixed) expressions...


you could do this and it would be logical again

#include <stdio.h>
#define y 'y'
#define n 'n'
#define m 'm'

int main (void)
{

    char input;
    input = getchar();
    switch (input)
    {
        case y: printf ("yes/n");
                break;
        case n: printf ("no/n");
                break;
        case m: printf ("menu/n");
                break;
        default: printf ("exit/n");
                 break;
    }
    
    return 0;
}


Was This Post Helpful? 1
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2513
  • View blog
  • Posts: 3,993
  • Joined: 21-June 11

Re: Switch Statement and Program

Posted 04 November 2017 - 12:51 PM

You could, but you really shouldn't. That's incredibly confusing and if you ever do try to create variables with those names, the error message will be confusing as well.
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1378
  • View blog
  • Posts: 4,320
  • Joined: 20-March 10

Re: Switch Statement and Program

Posted 04 November 2017 - 01:05 PM

Yes possibly...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1