dimanche 29 décembre 2019

Function that changes number in string to int

I'm trying to code a function in c that changes a number in string to an integer. For example "one" to 1. This is my code so far but it is not working how its supposed to and I can't find the problem.

int toint(char value[10], int num){

    if(strcmp(value, "Zero") == 0){
        num = 0;
    }else if(strcmp(value, "One") == 0){
        num = 1;
    }else if(strcmp(value, "Two") == 0){
        num = 2;
    }else if(strcmp(value, "Three") == 0){
        num = 3;
    }else if(strcmp(value, "Four") == 0){
        num = 4;
    }else if(strcmp(value, "Five") == 0){
        num = 5;
    }else if(strcmp(value, "Six") == 0){
        num = 6;
    }else if(strcmp(value, "Seven") == 0){
        num = 7;
    }else if(strcmp(value, "Eight") == 0){
        num = 8;
    }else if(strcmp(value, "Nine") == 0){
        num = 9;
    }else if(strcmp(value, "Ten") == 0){
        num = 10;
    }

    return num;
}

This is where the function its called. It's being called from a for loop in another function so that I can sort an array of numbers in string in ascending order.

    int lastval;

    toint(array[i-1], lastval);

    printf("%d", lastval);

    int val = rand() % 10 + lastval;

Aucun commentaire:

Enregistrer un commentaire