Error when printing string

Asked

Viewed 105 times

0

#include<stdio.h>
int main(){
    int numero,unidade,dezena,centena;
    char *unidades[]={"I","II","III","IV","V","VI","VII","VIII","IX"}; 
    char *dezenas[]={"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
    char *centenas[]={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
    scanf("%d",&numero);
    unidade=(numero%100)%10;
    dezena=(numero%100)/10;
    centena=numero/100;
    if (centena){
        printf("%s",centena[centena-1]);
    }
    if (dezena){
        printf("%s",dezena[dezena-1]);
    }
    if (unidade){
        printf("%s",unidade[unidade-1]);
    }
    printf("\n");
    return 0;
}

So, I was trying to make the 1960 URI, which consists of converting a decimal number to a number in Roman numerals, but I’m having trouble printing the string.

The following error occurs:

subscripted value is neither array nor Pointer nor vector printf("%s",hundred[hundred-1]);

The same occurs for dozens and units.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

The problem is you forgot s in the names of vectors:

#include <stdio.h>

int main() {
    char *unidades[] = {"I", "II", "III" ,"IV", "V", "VI", "VII", "VIII", "IX"}; 
    char *dezenas[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    char *centenas[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    int numero;
    scanf("%d", &numero);
    int unidade = (numero % 100) % 10;
    int dezena = (numero % 100) / 10;
    int centena = numero / 100;
    if (centena) printf("%s", centenas[centena - 1]);
    if (dezena) printf("%s", dezenas[dezena - 1]);
    if (unidade) printf("%s", unidades[unidade - 1]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • My stupidity, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.