help

This is a discussion on help within the C Programming forums, part of the General Programming Boards category; this program so far is ment to prompt for and accept 10 values, store them in an array and then ...

  1. #1
    kiz
    kiz is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    help

    this program so far is ment to prompt for and accept 10 values, store them in an array and then print them out.. the only problem i have is that when it prompts for the first value, it doesnt accept it straight away. the program usage is shown below.. i had to key in 1 twice before it prompted for value 2 and so it screwed up the storage because value 2 becomes value 3 and value 3 becomes value 4 etc

    Please enter 10 values

    Value 1?
    1
    1
    Value 2?
    3
    Value 3?
    4
    Value 4?
    6
    Value 5?
    2
    Value 6?
    7
    Value 7?
    9
    Value 8?
    4
    Value 9?
    2
    Value 10?
    13

    1. 1.000000
    2. 1.000000
    3. 3.000000
    4. 4.000000
    5. 6.000000
    6. 2.000000
    7. 7.000000
    8. 9.000000
    9. 4.000000
    10. 2.000000

    and this is my code

    Code:
    int main(int argc, char * argv[])
    {
     // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    float array[10];
    int i;
    int no=1;
    
    fprintf(output,"Please enter 10 values\n");
    
            for (i=0;i<10;i++)
                    {
                    fprintf(output,"Value %d?\n",no);
                    fscanf(input,"%f\n",&array[i]);
                    no++;
                    }
    no=1;
            for (i=0;i<10;i++)
                    {
                    fprintf(output,"%d. %f\n",no,array[i]);
                    no++;
                    }
    }
    does anyone know wat is wrong with this??

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    http://c-faq.com/stdio/scanfhang.html
    Code:
    fscanf(input,"%f\n",&array[i]);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

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