ints and doubles problem

This is a discussion on ints and doubles problem within the C Programming forums, part of the General Programming Boards category; I'm trying to accept doubles from a text file called pay.txt. But it only seems to work with ints. When ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    1

    ints and doubles problem

    I'm trying to accept doubles from a text file called pay.txt. But it only seems to work with ints. When I change my var types to doubles, It creates an endless loop. Here is my code:

    #include<stdio.h>

    main(){

    double pay_arr[5], sum;
    double avg, pay, x;
    FILE *inpay;

    inpay = fopen("pay.txt", "r");

    sum = 0;
    for(x=0; x<6; x++){
    while( fscanf(inpay,"%d ", &pay_arr[x]) != EOF){
    printf("%d\n", pay_arr[x]);
    sum = sum + pay_arr[x];
    }
    }
    printf("total: %.2lf\n avg: %.2lf\n", sum, sum/6);

    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,199
    First off, use code tags .

    Second:
    check what datatype fscanf is expecting, and the following printf in the loop, and check what datatype you send it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting strings to ints
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 06:08 PM

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