Invalid conversion from “char” to “char *”

拥有回忆 提交于 2019-12-10 12:24:43

问题


This is the program to convert a Roman number (for example VI) to a decimal. The algorithm is writing a function that recognize each element of the input string and return the corresponding value in decimal.

We got char Roman_num[20] For each element, sum+=value(Roman_num[i]).The function prototype is int value (char digit).It results in the 'Invalid conversion from char to char *' error.

However, when passing each element's address &a[i] to the function and changing the prototype to int value (char *digit), it doesn't repeat this error but leads to another error in this switch-case (inside the function) : switch (*digit) gives an error of 'digit' cannot appear in a constant-expression

My question is I was not clear that: in this example, do we have to pass only the address to the funcion? If we want to pass the value, a single character value, then how? Last but not least, *digit is actually a single character, then why it cannot appear in a constant-expression in case()?

I will be grateful for your help. On the other hand, can you please recommend me some books for deep understanding in C? I'm now using C : How To Program book, and I hardly know about how the variables, functions working on the inside for a deeper understanding.

This is the code:

int value (char *digit);
int main (void)
{
    char a[100];
    int length,i,sum=0;
    printf("Enter your Roman number: ");
    fflush(stdin);
    gets(a);
    printf("\nThe Roman number that you have entered is %s",a);
    length=strlen(a);
    for (i=0;i<length;i++)
    {
        sum+=value(&a[i]);
    }
    printf("\nthen it is: %d",sum);
    getch();
    return 0;
}
int value (char *digit)
{
    int num;
    case ( *digit ){
        case 'M':
            num=1000;
            break;
        case 'D':
            num=500;
            break;
        case 'C':
            num=100;
            break;
        case 'L':
            num=50;
            break;
        case 'X':
            num=10;
            break;
        case 'V':
            num=5;
            break;
        case 'I':
            num=1;
            break;
    }
    return num;
}

回答1:


Since you will not post code, here some code for getting roman numerals that always increase. The subtraction part is left to you to figure out. I only post this as it sounds like you are self teaching yourself which is comendable:


int romanToValue(const char c)
{
    switch(c)  // Only works with upper-case as lower case means different things.
    {
      case 'I' : return 1;
      case 'V' : return 5;
      case 'X' : return 10;
      case 'L' : return 50;
      case 'C' : return 100;
      case 'D' : return 500;
      case 'M' : return 1000;
      default: printf("Bad value in string %c\n", c); break;  
    }

    return 0;
}

int romanToInt(const char *str)
{
  int value = 0;
  int i;

  for(i=0; str[i]; i++)  // Dangerous way to do strings, but works for a C example.
  {
    value += romanToValue(str[i]);
  }

  return value;
}

int main(void)
{
  const char cstr[] = "VIII";

  printf("value:%d\n", romanToInt(cstr));
  return 0;
}

Notice the switch statement is working off of char values.



来源:https://stackoverflow.com/questions/16739940/invalid-conversion-from-char-to-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!