Jordan Lindsay
Jordan Lindsay

Reputation: 33

C program immediately exits after run?

Hey I have a real quick question about a program I've written. For some reason it's exiting immediately after it runs. There are no errors when I hit run, but something's obviously wrong.

Because I can't get it to run at all, I can't test it to make sure it's doing exactly what I need it to.

Here's the code:

#include <stdio.h>
#include <stdlib.h>
#define MAXCHARS 31

struct TempInfo
    {
        char city[MAXCHARS];
        int day;
        double fahr;
        double cels;
    };

void input_TempInfo(struct TempInfo * arr, int size);
void print_TempInfo(struct TempInfo * arr, int size);

void main(int argc, char *argv[])
{
    int size;
    struct TempInfo arr[size];
    struct TempInfo * arrPtr;
    arrPtr = arr;

    do
    {
        printf("How many cities / days / temps would you like to enter? ");
        scanf("%i", &size);
        if (size < 1 || size > 20)
        {
            printf("Not a valid number. Type another: ");
            scanf("%i", &size);
        }
    } while (size >= 1 && size <= 20);

    input_TempInfo(arrPtr, size);
    print_TempInfo(arrPtr, size);

    return 0;
}

void input_TempInfo(struct TempInfo * arr, int size)
{
    int i, k;

    for (i = 0, k = 0; k < size; i++, k++)
    {
        printf("Enter the city: ");
        while (((arr[k].city[i] = getchar()) != '\n') && (i < MAXCHARS))
            i++;
        arr[k].city[i] = '\0';

        do
        {
            printf("Enter the day: ");
            scanf("%i", &arr[k].day);
            if (arr[k].day < 1 || arr[k].day > 366)
            {
                printf("Invalid day number. Type another: ");
                scanf("%i", &arr[k].day);
            }
        } while (arr[k].day >= 1 && arr[k].day <= 366);

        printf("Enter a temperature in Fahrenheit: ");
        scanf("%lf", &arr[k].fahr);

        arr[k].cels = (arr[k].fahr - 32) * 5/9;
    }
}

void print_TempInfo(struct TempInfo * arr, int size)
{
    int i, k;

    for (i = 0, k = 0; k < size; i++, k++)
    {
        printf("City\tDay\tFahr\tCels");
        printf("------------------------------");
        printf("%s\t%i\t%.1lf\t%.1lf",
            arr[k].city, arr[k].day, arr[k].fahr, arr[k].cels);
    }
}

EDIT I rearranged my declarations of my 'arr[size]' and '* arrPtr' to be after the 'do while' And it worked better. Thank you guys so much.

Upvotes: 1

Views: 151

Answers (1)

This code chunk

int size;
struct TempInfo arr[size];
struct TempInfo * arrPtr;
arrPtr = arr;

is wrong: you are using a VLA (variable length array, here arr) of uninitialized size

I was hoping that if you compile with all warnings and debug info (gcc -Wall -Wextra -g if using GCC) you would have been warned. Unfortunately, that is not the case (with GCC 4.9), unless you also ask for optimization (so with gcc -Wall -Wextra -O -g I am getting a warning). so please submit a bug on GCC bugzilla

At least, move the do ... while(size >= 1 && size <= 20); loop before that chunk. Read the documentation of every function you are using. Notice that scanf(3) is returning the count of successfully scanned items, and you should test it!

BTW, you should learn how to use the debugger (e.g. gdb)

Upvotes: 7

Related Questions