Case statement

Discussion in 'C++' started by deanfamily11, Sep 10, 2005.

  1. deanfamily11

    deanfamily11 Guest

    I've set up a case statement to have my program determine where on the
    Cartesian plane a point the user enters is located. I keep getting the
    C2051 error when I compile. Any help?


    #include <iomanip>
    #include <iostream>

    using namespace std;

    int main()
    {

    //declare variables
    int var1, var2;

    //get the points
    cout << "Enter 2 numbers that are the coordinates on a Cartesian plane: ";
    cin >> var1 >> var2;
    cout << endl << endl;

    //use a case statment to determine the plane
    switch (var1 & var2)
    {
    case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<
    ")"
    << " is on the origin." << endl;
    break;
    case ((var1 >= 1) || (var1 <= -1)) && (var2 == 0):
    cout << "(" << var1 << "," << var2 << ")" << "is on the x axis." <<
    endl;
    break;
    case (var1 == 0) && ((var2 >= 1) || (var2 <= -1)):
    cout << "(" << var1 << "," << var2 << ")" << "is on the y axis." <<
    endl;
    break;
    case (var1 >= 1) && (var2 >= 1): cout << "(" << var1 << "," << var2 <<
    ")"
    << " is in the first quadrant." << endl;
    break;
    case (var1 <= -1) && (var2 >= 1): cout << "(" << var1 << "," << var2 <<
    ")"
    << " is in the second quadrant." << endl;
    break;
    case (var1 <= -1) && (var2 <= -1):cout << "(" << var1 << "," << var2 <<
    ")"
    << " is in the third quadrant." << endl;
    break;
    case (var1 >= 1) && (var2 <= -1): cout << "(" << var1 << "," << var2 <<
    ")"
    << " is in the fourth quadrant." << endl;
    break;
    }

    return 0;
    }
     
    deanfamily11, Sep 10, 2005
    #1
    1. Advertising

  2. deanfamily11

    Artie Gold Guest

    deanfamily11 wrote:
    > I've set up a case statement to have my program determine where on the
    > Cartesian plane a point the user enters is located. I keep getting the
    > C2051 error when I compile. Any help?


    An `error number' is specific to your compiler -- and meaningless in
    this context.
    >
    >
    > #include <iomanip>
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main()
    > {
    >
    > //declare variables
    > int var1, var2;
    >
    > //get the points
    > cout << "Enter 2 numbers that are the coordinates on a Cartesian plane: ";
    > cin >> var1 >> var2;
    > cout << endl << endl;
    >
    > //use a case statment to determine the plane
    > switch (var1 & var2)


    Somehow, I suspect that you don't want the bitwise `and' of the
    variables that were input.

    > {
    > case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<
    > ")"

    Case labels must be constants.

    *Please* get an appropriate book (see http://www.accu.org for
    possibilities) -- and study. You, as of yet, have no clue as to the
    syntax of C++.

    > [snip]


    HTH and Cheers,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
     
    Artie Gold, Sep 10, 2005
    #2
    1. Advertising

  3. deanfamily11

    GB Guest

    deanfamily11 wrote:
    > I've set up a case statement to have my program determine where on the
    > Cartesian plane a point the user enters is located. I keep getting the
    > C2051 error when I compile. Any help?


    Error codes are compiler-specific, so in the future you should indicate
    the actual error message, not the code. One problem you have is that you
    are using runtime expressions in your case statements. Each case in a
    switch statement must contain a distinct compile-time constant. You
    cannot use expressions that are evaluated at runtime.

    > switch (var1 & var2)


    This is technically okay, but I doubt you want to use the & operator.

    > {
    > case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<


    This is not okay. You will need to use a nested if statement to do this.

    Gregg
     
    GB, Sep 10, 2005
    #3
  4. deanfamily11

    deanfamily11 Guest

    Well, then the text of the error is "case expression not constant" and it
    occurs on every line of the case statement.

    "Artie Gold" <> wrote in message
    news:...
    > deanfamily11 wrote:
    >> I've set up a case statement to have my program determine where on the
    >> Cartesian plane a point the user enters is located. I keep getting the
    >> C2051 error when I compile. Any help?

    >
    > An `error number' is specific to your compiler -- and meaningless in this
    > context.
    >>
    >>
    >> #include <iomanip>
    >> #include <iostream>
    >>
    >> using namespace std;
    >>
    >> int main()
    >> {
    >>
    >> //declare variables
    >> int var1, var2;
    >>
    >> //get the points
    >> cout << "Enter 2 numbers that are the coordinates on a Cartesian plane:
    >> ";
    >> cin >> var1 >> var2;
    >> cout << endl << endl;
    >>
    >> //use a case statment to determine the plane
    >> switch (var1 & var2)

    >
    > Somehow, I suspect that you don't want the bitwise `and' of the variables
    > that were input.
    >
    >> {
    >> case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2
    >> << ")"

    > Case labels must be constants.
    >
    > *Please* get an appropriate book (see http://www.accu.org for
    > possibilities) -- and study. You, as of yet, have no clue as to the syntax
    > of C++.
    >
    > > [snip]

    >
    > HTH and Cheers,
    > --ag
    >
    > --
    > Artie Gold -- Austin, Texas
    > http://goldsays.blogspot.com (new post 8/5)
    > http://www.cafepress.com/goldsays
    > "If you have nothing to hide, you're not trying!"
     
    deanfamily11, Sep 10, 2005
    #4
  5. deanfamily11

    M Guest

    On Sat, 10 Sep 2005 03:51:48 GMT, "deanfamily11"
    <> wrote:

    >I've set up a case statement to have my program determine where on the
    >Cartesian plane a point the user enters is located. I keep getting the
    >C2051 error when I compile. Any help?
    >


    > //use a case statment to determine the plane
    > switch (var1 & var2)
    > {
    > case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<


    You can't do that...

    The expressions in a case statement must evaluate to a constant
    integer at compile time.

    The switch expression will compile, but will not work as you expect,
    because it will also be evaluated as an integer, but at run time.
     
    M, Sep 10, 2005
    #5
  6. deanfamily11 wrote:
    > Well, then the text of the error is "case expression not constant" and it
    > occurs on every line of the case statement.
    >


    Well exactly

    case 1:
    case 2:
    case 3:
    case 4:

    these are OK, the case expressions are constants

    case (var1 == 0) && (var2 == 0):

    this is not OK, the case expression is not a constant.

    You want an if statement not a case statement

    if ((var1 == 0) && (var2 == 0))
    {
    ...
    }
    else if (((var1 >= 1) || (var1 <= -1)) && (var2 == 0))
    {
    ...
    }

    etc.

    john
     
    John Harrison, Sep 10, 2005
    #6
  7. deanfamily11

    Jim Langston Guest

    "deanfamily11" <> wrote in message
    news:eek:LsUe.576$vQ3.44@trnddc08...
    > I've set up a case statement to have my program determine where on the
    > Cartesian plane a point the user enters is located. I keep getting the
    > C2051 error when I compile. Any help?
    >
    >
    > #include <iomanip>
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main()
    > {
    >
    > //declare variables
    > int var1, var2;
    >
    > //get the points
    > cout << "Enter 2 numbers that are the coordinates on a Cartesian plane:
    > ";
    > cin >> var1 >> var2;
    > cout << endl << endl;
    >
    > //use a case statment to determine the plane
    > switch (var1 & var2)
    > {
    > case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<
    > ")"
    > << " is on the origin." << endl;
    > break;
    > case ((var1 >= 1) || (var1 <= -1)) && (var2 == 0):
    > cout << "(" << var1 << "," << var2 << ")" << "is on the x axis." <<
    > endl;
    > break;
    > case (var1 == 0) && ((var2 >= 1) || (var2 <= -1)):
    > cout << "(" << var1 << "," << var2 << ")" << "is on the y axis." <<
    > endl;
    > break;
    > case (var1 >= 1) && (var2 >= 1): cout << "(" << var1 << "," << var2 <<
    > ")"
    > << " is in the first quadrant." << endl;
    > break;
    > case (var1 <= -1) && (var2 >= 1): cout << "(" << var1 << "," << var2 <<
    > ")"
    > << " is in the second quadrant." << endl;
    > break;
    > case (var1 <= -1) && (var2 <= -1):cout << "(" << var1 << "," << var2 <<
    > ")"
    > << " is in the third quadrant." << endl;
    > break;
    > case (var1 >= 1) && (var2 <= -1): cout << "(" << var1 << "," << var2 <<
    > ")"
    > << " is in the fourth quadrant." << endl;
    > break;
    > }
    >
    > return 0;
    > }
    >


    You have case statement syntax wrong, and it won't work for what you want.

    it's:
    switch ( expression ) statement
    case constant-expression : statement
    default : statement

    Notice the case takes a constant-expression. == is already "built in".

    Such as:
    switch( c )
    {
    case 'A':
    capa++;
    break;
    case 'a':
    lettera++;
    break;
    default :
    total++;
    }

    Notice, NOT case ( c == 'a' ), but case 'A':

    You could do more than one...
    case 'A':
    case 'B':
    case 'C':
    std::cout << "A to C" << std:endl;
    break;

    So, it's not going to do what you want. A case statement can't check for
    >=, only ==

    You'll need to use some other form of construct, I'd use if statements, and,
    to be truthful, your whole block is in the form of a big if statement block
    anyway with a little format adjusting.

    if ( (var1 == 0) && (var2 == 0) )
    cout << "(" << var1 << "," << var2 << ")"
    << " is on the origin." << endl;
    else if ( ((var1 >= 1) || (var1 <= -1)) && (var2 == 0) )
    cout << "(" << var1 << "," << var2 << ")" << "is on the x axis." <<
    endl;
    else if ( (var1 == 0) && ((var2 >= 1) || (var2 <= -1)) )
    cout << "(" << var1 << "," << var2 << ")" << "is on the y axis." <<
    endl

    etc...
     
    Jim Langston, Sep 11, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Franks
    Replies:
    2
    Views:
    1,257
    Steve Franks
    Jun 10, 2004
  2. Tee
    Replies:
    3
    Views:
    7,815
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  3. Janice

    lower case to upper case

    Janice, Dec 10, 2004, in forum: C Programming
    Replies:
    17
    Views:
    1,181
    Richard Bos
    Dec 14, 2004
  4. Replies:
    1
    Views:
    2,482
    Mark P
    Apr 6, 2007
  5. darrel

    'case' vs. 'case is'

    darrel, Aug 14, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    275
    Rory Becker
    Aug 14, 2008
Loading...

Share This Page