having trouble with variables

This is a discussion on having trouble with variables within the C++ Programming forums, part of the General Programming Boards category; I have the following lines in my program: for(i=0; i<rows; i++) { fscanf(input, "%lf %lf", &time[i], &value[i]); printf("%lf %lf\n", time[i], ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    having trouble with variables

    I have the following lines in my program:


    for(i=0; i<rows; i++)
    {
    fscanf(input, "%lf %lf", &time[i], &value[i]);
    printf("%lf %lf\n", time[i], value[i]);
    }

    I want j (type double) to be the difference between the second and first values in time.

    j=time[1]-time[0];

    However, when I do that, j turns out to be close to 2000000000 every time. What am I doing incorrectly?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    Here is my program in its entirety thus far:

    void main(void)
    {
    //variables
    char file_in[25];
    char str[90];
    int i=0, rows=0;
    double j;

    // Pointers
    double *time;
    double *value;
    FILE *input;

    // Load incoming file
    cout<<"Please enter the name of the file to load.\n";
    cin>>file_in;

    // Determination of number of rows
    ifstream a_file;
    a_file.open(file_in);

    while(!a_file.eof()) {
    a_file.getline(str, 90);
    rows++;}

    a_file.close();
    input=fopen(file_in, "r");

    // Allocate memory for array

    time=(double *)malloc(rows*sizeof(double));
    value=(double *)malloc(rows*sizeof(double));

    // Determine values for input data and store in an array

    for(i=0; i<rows; i++) {
    fscanf(input, "%lf %lf", &time[i], &value[i]);
    printf("%lf %lf\n", time[i], value[i]); }
    rewind(input);

    // Determine time interval between measurements

    j=time[1]-time[0];
    printf("The time between intervals is: %d", j);
    }

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    Figured it out, changing j from double to float fixed it (duh).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  3. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  4. Having trouble correctly setting DJGPP's env. variables in XP
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 06-05-2002, 11:51 PM
  5. I'm having trouble passing variables with pointers
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2002, 12:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21