user14551695
user14551695

Reputation:

Strtol function giving strange logic errors

I'm creating a program that inputs six strings and converts them to integers, adds them, and outputs the result.

However, the program sometimes outputs the correct result, but strangely, it can also output 30 when the total should have been 28 or 29.

// Inputs six strings and converts them to integers for calculations
#include <stdio.h>
#include <stdlib.h>
#define AMOUNT 6
#define SIZE 12

int main(void)
{
    // string array where inputs are stored
    char *inputs[7] = {"\0\0\0\0\0\0\0\0\0\0\0"};
    
    // get six strings representing integers
    for (unsigned short int counter = 0; counter < AMOUNT; counter++) {
        printf("%s", "Enter an integer: ");
        
        fgets(&inputs[counter], SIZE, stdin);
        
        puts("");
    }
    
    // long array where converted numbers are stored
    long convertedInteger[6];
    
    // pointer to convert integers
    char *Ptr;
    
    for (unsigned short int counter = 0; counter < AMOUNT; counter++) {
        convertedInteger[counter] = strtol(inputs, &Ptr, 0);
    }
    
    // output variable
    long long int output = 0;
    
    // loop to add integers to total
    for (unsigned short int counter = 0; counter < AMOUNT; counter++) {
        output += convertedInteger[counter];
    }
    
    // print output variable
    printf("The total is %lld", output);
}

Edit: Here's the warnings I'm receiving. Some of them don't seem to be coming from my program. I'm using Dev-C++ 5.11 and compiling to C11.

list of program warnings

Upvotes: 0

Views: 64

Answers (2)

user14551695
user14551695

Reputation:

I fixed the problem by making the input variable a multidimensional char array. I'll put the modified code below.

// Inputs six strings and converts them to integers for calculations
#include <stdio.h>
#include <stdlib.h>
#define AMOUNT 6
#define SIZE 12

int main(void)
{
    // string array where inputs are stored
    char inputs[AMOUNT][SIZE];
    
    // get six strings representing integers
    for (unsigned short int counter = 0; counter < AMOUNT; counter++) {
        printf("%s", "Enter an integer: ");
        
        fgets(inputs[counter], SIZE, stdin);
        
        puts("");
    }
    
    // long array where converted numbers are stored
    long convertedInteger[6];
    
    for (unsigned short int counter = 0; counter < AMOUNT; counter++) {
        char *convertedPtr;
            
        convertedInteger[counter] = strtol(inputs[counter], &convertedPtr, 0);
    }
    
    // output variable
    long long int output = 0;
    
    // loop to add integers to total
    for (unsigned short int counter = 0; counter < AMOUNT; counter++) {
        output += convertedInteger[counter];
    }
    
    // print output variable
    printf("The total is %lld", output);
}

Upvotes: 1

Murphy
Murphy

Reputation: 3999

Edit: Obsoleted by Q edit.

long long int output;

Basic beginner's error: Uninitialized variable.

Upvotes: 1

Related Questions