Thread: Need a little help.

  1. #1
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335

    Need a little help.

    I'm trying to make a program that tells you how much your worth in gold. Here is the code:
    Code:
    /* goldyou.c -- the worth of your weight in gold */
    
    #include <stdio.h>
    
    int main(void)
    {
        float weight;         /*users weight*/
        float value;          /*user's gold equivalent*/
    
        printf("Are you worth your weight in gold?\n");
        printf("Let's find out.\n");
        printf("Please enter your weight in pounds: ");
    
    /* get input from user */
        scanf("%f", &weight);
    /* gold is worth $320 per ounce */
    /* 14.5833 converts pounds to ounces */
        value = 320.0 * weight * 14.5833;
        printf("Your weight in gold is worth $%.2f.\n", value);
        printf("You are easily worth that!\n");
        printf("If gold prices drop, eat more to maintain your value.\n");
        getchar();
        return 0;
    }
    The program starts fine and lets me enter my weight. After I do and press enter the screen disappears. Any ideas?

    -Fool

  2. #2
    Unregistered
    Guest

    Wink

    Try something like this:

    #include <iostream.h>
    #include <stdio.h>

    main()
    {
    float weight, value;
    cout<<"Are you worth your weight in gold?\n";
    cout<<"Let's find out.\n";
    cout<<"Please enter your weight in pounds: ";
    cin>>weight;
    value = 320 * weight * 14.5833;
    cout<<"Your weight in gold is worth "<<value<<endl;
    cout<<"You are easily worth that!\n";
    cout<<"If gold prices drop, eat more to maintain your value.\n";
    getchar();
    return 0;
    }

    OK?

    are you sure about those 320$???

    -Ice- (IcyDeath)

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi,
    After rearding this post and previous post, your problem is as what has been said, but instead of using getchar(); or getch(); with DevC++ I use printf("Enter any value to Exit :"); and then use any appropraite variable in my program and use scanf to input exit value....This allows you to see your final program before exiting....
    Luckily, my days with devC++ are finished...roll on TurboC (borland) kindly supplied by my course supplier...now I am able to implement conio.h in full...and also TurboC allows you to veiw final statements before closing....and has a normal window interface..
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Originally posted by Unregistered
    Try something like this:

    #include <iostream.h>
    #include <stdio.h>

    main()
    {
    float weight, value;
    cout<<"Are you worth your weight in gold?\n";
    cout<<"Let's find out.\n";
    cout<<"Please enter your weight in pounds: ";
    cin>>weight;
    value = 320 * weight * 14.5833;
    cout<<"Your weight in gold is worth "<<value<<endl;
    cout<<"You are easily worth that!\n";
    cout<<"If gold prices drop, eat more to maintain your value.\n";
    getchar();
    return 0;
    }

    OK?

    are you sure about those 320$???

    -Ice- (IcyDeath)
    I need to only use C. The book I am studying from said $320. It's just for practice so it doesn't really matter.

    -Fool

  5. #5
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Ok, I added another printf and scanf to exit. Here's what I've got:
    Code:
    /* goldyou.c -- the worth of your weight in gold */
    
    #include <stdio.h>
    
    int main(void)
    {
        float weight;         /*users weight*/
        float value;          /*user's gold equivalent*/
        float exit;           /*exit program*/
    
        printf("Are you worth your weight in gold?\n");
        printf("Let's find out.\n");
        printf("Please enter your weight in pounds: ");
    
    /* get input from user */
        scanf("%f", &weight);
    /* gold is worth $320 per ounce */
    /* 14.5833 converts pounds to ounces */
        value = 320.0 * weight * 14.5833;
        printf("Your weight in gold is worth $%.2f.\n", value);
        printf("You are easily worth that!\n");
        printf("If gold prices drop, eat more to maintain your value.\n");
        printf("Please press enter to exit program: ");
        scanf("%f", &exit);
        return 0;
    }
    Now it will run the entire program but when you press enter to exit it will not exit. How can I do this?

    -Fool

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    maybe better to declare exit as an int value....if that fails then just close screen normal...you will gwt warning but just click ok and it will exit...okay

    int exit;

    printf("Enter any value to exit :");
    scanf("%d",&exit);
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  7. #7
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Where would I add in the int exit; ?

    -Fool

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    I had the same sort of problem. The thing is that scanf leaves the \n character in the buffer, so when it gets to your getch, it takes out the \n and exits. Go here: http://www.cprogramming.com/cboard/s...=&threadid=191 to see how to solve it. (make sure to read the whole thing)

  9. #9
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you're using most compilers use <conio.h> and getch() but if you're using Dev C++ you can use conio_mingw.h to use getch().


    Code:
    #include     <conio_mingw.h>
    //Your includes
    
    
    int main()
    {
    
          //Your code
    
          getch();
          return 0;
    }

  10. #10
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Ok guys, I got it. Thanks for the help! Here's what it ended up looking like...
    Code:
    /* goldyou.c -- the worth of your weight in gold */
    
    #include <stdio.h>
    
    int main(void)
    {
        float weight;         /*users weight*/
        float value;          /*user's gold equivalent*/
        float exit;           /*exit the program*/
    
        printf("Are you worth your weight in gold?\n");
        printf("Let's find out.\n");
        printf("Please enter your weight in pounds: ");
    
    /* get input from user */
        scanf("%f", &weight);
    /* gold is worth $320 per ounce */
    /* 14.5833 converts pounds to ounces */
        value = 320.0 * weight * 14.5833;
        printf("Your weight in gold is worth $%.2f.\n", value);
        printf("You are easily worth that!\n");
        printf("If gold prices drop, eat more to maintain your value.\n");
        getch();
        return 0;
    }
    -Fool

  11. #11
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    But isn't getch just a macro for getchar, zen?

  12. #12
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    In the conio.h that comes with Dev C++ it is, yeah. But I think most copies also have the Mingw implementation in which getch() behaves as it does on most other compilers.

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi
    as you know on the DOS/Windows platforms pressing the Enter key generates two symbols: <CR><LF>
    so when you press the enter when filling the weight variable
    the one <LF >left in the input stream so the getchar() eat it and exit immediately.

    when you use the second 'scanf' it didn't exit becouse there is a simple validation used to enshure that there will be an nonempty string to be evaluated before the function return the value.

    damyan

Popular pages Recent additions subscribe to a feed