CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1

    my program is blowing it

    I am doing something wrong with these floating point arrays... it crashes when it trys to scanf the first value and store it in the value [0]




    #include <stdio.h>


    int get_values();
    int print_values();

    float values[5];
    char stock_names[5][10];





    int main ()


    {


    get_values();
    print_values();

    return (0);

    }




    int get_values()
    {


    printf("Please Enter a stock name: ");
    scanf(" %s", stock_names[1]);
    printf("\nPlease enter the daily stock average of stock 1: ");
    scanf(" %f", values[0]);

    printf("\n\nPlease Enter a stock name: ");
    scanf(" %s", stock_names[2]);
    printf("\nPlease enter the daily stock average of stock 2: ");
    scanf(" %f", values[1]);

    printf("\n\nPlease Enter a stock name: ");
    scanf(" %s", stock_names[3]);
    printf("\nPlease enter the daily stock average of stock 3: ");
    scanf(" %f", values[2]);

    printf("\n\nPlease Enter a stock name: ");
    scanf(" %s", stock_names[4]);
    printf("\nPlease enter the daily stock average of stock 4: ");
    scanf(" %f", values[3]);

    printf("\n\nPlease Enter a stock name: ");
    scanf(" %s", stock_names[5]);
    printf("\nPlease enter the daily stock average of stock 5: ");
    scanf(" %f", values[4]);

    return (0);


    }


    int print_values()


    {


    printf("%s \t%.2f\n", stock_names[1], values[0]);
    printf("%s \t%.2f\n", stock_names[2], values[1]);
    printf("%s \t%.2f\n", stock_names[3], values[2]);
    printf("%s \t%.2f\n", stock_names[4], values[3]);
    printf("%s \t%.2f\n", stock_names[5], values[4]);


    return (0);
    }

  2. #2
    Join Date
    Mar 2004
    Location
    100m below sea level
    Posts
    189
    I think that stock_names has the problem there.

    the array index starts with 0... so it should be :

    int get_values()
    {


    printf("Please Enter a stock name: ");
    scanf(" %s", stock_names[0]);
    printf("\nPlease enter the daily stock average of stock 1: ");
    scanf(" %f", values[0]);

    printf("\n\nPlease Enter a stock name: ");
    scanf(" %s", stock_names[1]);
    printf("\nPlease enter the daily stock average of stock 2: ");
    scanf(" %f", values[1]);

    printf("\n\nPlease Enter a stock name: ");
    scanf(" %s", stock_names[2]);
    printf("\nPlease enter the daily stock average of stock 3: ");
    scanf(" %f", values[2]);

    printf("\n\nPlease Enter a stock name: ");
    scanf(" %s", stock_names[3]);
    printf("\nPlease enter the daily stock average of stock 4: ");
    scanf(" %f", values[3]);

    printf("\n\nPlease Enter a stock name: ");
    scanf(" %s", stock_names[4]);
    printf("\nPlease enter the daily stock average of stock 5: ");
    scanf(" %f", values[4]);

    return (0);


    }


    int print_values()


    {


    printf("%s \t%.2f\n", stock_names[0], values[0]);
    printf("%s \t%.2f\n", stock_names[1], values[1]);
    printf("%s \t%.2f\n", stock_names[2], values[2]);
    printf("%s \t%.2f\n", stock_names[3], values[3]);
    printf("%s \t%.2f\n", stock_names[4], values[4]);


    return (0);
    }
    Come Join This Poll Where are we from? (Ultimate)

    Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
    An idle brain is the devil's workshop. - unknown

  3. #3
    Changed it...


    still c rashes as im entering the first into value


    any other guesses?

  4. #4
    Join Date
    Mar 2004
    Location
    100m below sea level
    Posts
    189
    i chaged this, and it works for me..
    scanf(" %.2f", values[0]);
    Come Join This Poll Where are we from? (Ultimate)

    Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
    An idle brain is the devil's workshop. - unknown

  5. #5
    Allright i tried that...

    and it works except... when i print out the elements of the value floating point array...

    i get 0.00

    every time

    also i figured out i have to fflush(stdin); after each scanf

    here look at my new code ... compile it... tell me what you think




    #include <stdio.h>


    int get_values();
    int print_values();

    float values[5];
    char stock_names[5][25];





    int main ()


    {


    get_values();
    print_values();

    return (0);

    }




    int get_values()
    {


    printf("Please Enter a stock name: (no spaces) ");
    scanf(" %s", stock_names[0]);
    printf("\nPlease enter the daily stock average of stock 1: ");
    scanf(" %.2f", values[0]);
    fflush(stdin);

    printf("\n\nPlease Enter a stock name: (no spaces) ");
    scanf(" %s", stock_names[1]);
    printf("\nPlease enter the daily stock average of stock 2: ");
    scanf(" %.2f", values[1]);
    fflush(stdin);

    printf("\n\nPlease Enter a stock name: (no spaces) ");
    scanf(" %s", stock_names[2]);
    printf("\nPlease enter the daily stock average of stock 3: ");
    scanf(" %.2f", values[2]);
    fflush(stdin);

    printf("\n\nPlease Enter a stock name: (no spaces) ");
    scanf(" %s", stock_names[3]);
    printf("\nPlease enter the daily stock average of stock 4: ");
    scanf(" %.2f", values[3]);
    fflush(stdin);

    printf("\n\nPlease Enter a stock name: (no spaces) ");
    scanf(" %s", stock_names[4]);
    printf("\nPlease enter the daily stock average of stock 5: ");
    scanf(" %.2f", values[4]);
    fflush(stdin);

    return (0);


    }


    int print_values()


    {


    printf("%s \t%.2f\n", stock_names[0], values[0]);
    printf("%s \t%.2f\n", stock_names[1], values[1]);
    printf("%s \t%.2f\n", stock_names[2], values[2]);
    printf("%s \t%.2f\n", stock_names[3], values[3]);
    printf("%s \t%.2f\n", stock_names[4], values[4]);


    return (0);
    }

  6. #6
    Join Date
    Mar 2004
    Location
    100m below sea level
    Posts
    189
    Ok. I got it. lets change your code like this.

    printf("Please Enter a stock name: ");
    scanf(" %s", stock_names[0]);
    printf("\nPlease enter the daily stock average of stock 1: ");
    scanf(" %f", &values[0]);
    fflush(stdin);
    Come Join This Poll Where are we from? (Ultimate)

    Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
    An idle brain is the devil's workshop. - unknown

  7. #7
    duh hahah
    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured