'Duplicate case value' error with chars.

This is a discussion on 'Duplicate case value' error with chars. within the C++ Programming forums, part of the General Programming Boards category; I want to give a variable a value according to the value of a char within a string. For example, ...

  1. #1
    Perma-Newbie
    Join Date
    Feb 2005
    Posts
    7

    'Duplicate case value' error with chars.

    I want to give a variable a value according to the value of a char within a string.

    For example, if (in[12] == z) y = 8;

    So what I've done is assign some const chars, and I'm using a switch... case to assign the values. I keep getting 'duplicate case value' errors from the line with "case (V): y = 5; break;" onwards, and my compiler tells me that the case value was previously used in the previous line.

    What am I doing wrong?

    Code:
        
    //...
    
        const char I = ('i'||'I');
        const char V = ('v'||'V');
        const char X = ('x'||'X');
        const char L = ('l'||'L');
        const char C = ('c'||'C');
        const char M = ('m'||'M');
        const char operators = ('/'||'*'||'+'||'-');
    
    //...
    
    for (i = 0, x = 0; i < 256, in[i] != '\0'; i++) 
            
            {
        
            if (in[i] != operators) {
                      switch (in[i]) {
                      
                             case (I): y = 1; break;
                             case (V): y = 5; break;
                             case (X): y = 10; break;
                             case (L): y = 50; break;
                             case (C): y = 100; break;
                             case (M): y = 1000; break;
                             default: cout<<"Oh ......... Something's gone wrong."; break;}
    
    /* code to do something with y before the cycle starts again and it's changed */
    
            }
    Yes, it's a roman numeral calculator .
    Last edited by Mr Newbie; 05-28-2005 at 10:36 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > const char I = ('i'||'I');
    This isn't what you think it is.
    In particular, it isn't a value which specifically matches two cases of the same letter.

    By the time you've done the same trick to several letters, you have duplicate values.

    Scrap all the const chars, and do this
    Code:
    switch ( toupper(in[i]) ) {
      case 'I': y = 1; break;
      case 'V': y = 5; break;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Perma-Newbie
    Join Date
    Feb 2005
    Posts
    7
    Thanks a lot, I didn't know of toupper.

    So what does "const char I = ('i'||'I');" do?

    Is it possible to assign multiple characters to one char at all? I suppose you could create a char array and iterate through it, applying code to each element, but this would be pretty messy.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > So what does "const char I = ('i'||'I');" do?
    Well it's a boolean expression, equivalent to
    Code:
    const char I = ( 'i' != 0 || 'I' != 0 );
    Since it's a logical OR expression with lots of things which are true, all your constants have the value 1.

    > Is it possible to assign multiple characters to one char at all?
    Not really
    You could do
    Code:
    if ( strchr( "Ii", in[i] ) != NULL )
    to see if a char was in a particular set of chars, but you couldn't use that in a case statement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 5
    Last Post: 03-05-2009, 10:32 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Can someone please clean up my code
    By ki113r in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 10:03 AM
  5. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21