2

So this function reads in integers from a file that are separated by spaces and gets the total. It works fine.

As long as the numbers are single digit, 1-9.
How do I make it read double digit numbers from 1-100 ?
Do I make a nested if within the ifs that read every number and then try to read a second number right next to it without space and then go for the third digit (case 100)?

And then I make them into an integer (which to be honest I have no idea how to do)

void get_total(FILE*fp){

char c;

int total=0;
int number_of_conversions;

number_of_conversions = fscanf(fp, "%c", &c);

while (isdigit(c) || isspace(c) && c !='\n' && c !=NULL){

    if (isspace(c)){
        number_of_conversions = fscanf(fp, "%c", &c);
    }
    else if (isdigit(c)){

        total = total + (c- '0');

        number_of_conversions = fscanf(fp, "%c", &c);

    } 

    number_of_conversions = fscanf(fp, "%c", &c);
}

printf("%d", total);

}

Killer_B
  • 61
  • 4
  • 9
  • 1
    Search something about the functions: fgets, strtok and strtol! I think is a good choise! (y). This program is reading chars not number. The file contains ascii representation of the digit. You may load a line using fgets, parse it to extract the strings that represent the numbers with strtok and convert the string in number using strtol. (y) – Sir Jo Black May 01 '15 at 09:40
  • 1
    i thought of a way to add the numbers. am not sure if it works or not. but simply if there are two digit numbers. multiply the first by 10 and add the second. or 3 (100) multiply the first by 100. the second by 10 and add the third. that should work. – Killer_B May 01 '15 at 09:41
  • 1
    sergio i haven't learned that yet so am not trying to do stuff i dont understand. – Killer_B May 01 '15 at 09:42
  • If you multiply the char 1 by 10 you obtain as result 490 if you use it as an int; if you use it as it's, a char, you obtain (490 & 255) where & is the logic and! because a char (normally) is only 8 bits! ... This is because the ASCII value of the char 1 is 49 (0x31)! – Sir Jo Black May 01 '15 at 09:43
  • If you read char by char you may try something like this: declare an int. For example: int a; Before to read the first char of a number you do: a=0, then for all char that is a number that you read until a space is reached you do this: a=a * 10 + (c - 48); ... then in the int a you have the number. – Sir Jo Black May 01 '15 at 09:50
  • no am converting the chars to ints before doing that. and it worked. my method worked. i simply checked if there is an integer right next to it. and if it is. check once more until white space. and use that thing in my first comment to get the total. ama friggin genias – Killer_B May 01 '15 at 10:07
  • 'c' is a character, so it will NEVER be equal NULL – user3629249 May 01 '15 at 21:34

2 Answers2

1

It depends if you intend (or are meant) to obtain the integers with a minimum of programming effort, or to practise or work out for yourself how to do (decimal) conversions.

If it is the former, you need to take a closer look at what fscanf can do for you: if you use a conversion specifier such as "%d" or "%i" you can input any integer in one call of fscanf.

If the latter, it make more sense to use fgetc(fp) – using fscanf(fp,"%c",&c) to read a single byte is very clumsy. In this case you are on the right track in your comment: just start with 0 and each time you find a (decimal) digit multiply by the integer result so far by 10 and add the new digit.

Comments on your code:

  • Comparing c to NULL is poor style, as NULL stands for a pointer, though it will compare equal to 0.
  • You seem to assume that c will be updated if you reach the end of the file.
  • You seem to stop at the end of the first line: have you tested your programme on multi-line files?
  • You should be checking whether fscanf returns EOF – always read the documentation of functions you do not know well!
  • You appear to read two characters each time through your loop, and only process one of them.
PJTraill
  • 1,353
  • 12
  • 30
  • yes the next step is for multi-line. but am not sure how to do that. though am sure the difficult part is over. any suggestions? – Killer_B May 01 '15 at 10:48
  • I hope you checked through my comments: the third is because of `'\n'` in your programme. – PJTraill May 01 '15 at 13:15
0

you might want to start by reducing that posted code to something simple like:

void get_total(FILE*fp)
{

    int c;

    int total=0;
    int value;

    while (fscanf(fp, "%c", &c) )
    {
        if( !isdigit(c) )
        { // then some kind of number separater found
            total += value;
            printf("value: %d, total: %d", value, total);
            value = 0;
        }

        else
        { // accumulate number
            value *= 10;      // make room for next digit
            value += c - '0'; // convert digit to int
        }
    }

    // print last value
    printf("last value: %d, final total: %d", value, total);
}
user3629249
  • 16,402
  • 1
  • 16
  • 17