Thread: ints and doubles problem

  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,218
    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