Thread: Simple problem

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    42

    Simple problem

    I am almost embarassed to be asking about something seemingly so simple, but I havent touched C in a while. What am I doing wrong here? I want the user to enter a number, and the result is that number /28 but I can't quite get it right.

    Thanks

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int number =0;
    float result;
    result = number/28;
    
    
    printf("How many boxes are in the shipment?\n\n");
    scanf("%f",&number);
    printf("You need %.2f flats\n\n",result);
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Try
    Code:
    result = number/28.0;
    Note in C:

    3/4 equals 0

    (float)3/4 equals 0.75

    3.0/4 equals 0.75

    3/4.0 equals 0.75

    Tim S.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    You are assigning to result before you've read in the value for number.
    Code:
    result = number/28;
    stores the result of the expression (number/28) in result, not the expression itself. So the result variable will not auto update when you change 'number'

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Hmmm, Im not getting errors now but I am getting some crazy results...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int number =0;
    float result;
    
    
    printf("How many boxes are in the shipment?\n\n");
    scanf("%f",&number);
    
    
    result = number/28.0;
    
    printf("You need %2.2f flats\n\n",result);
    
    return 0;
    
    }
    I changed the 28 to 28.0 and moved it down after the scanf() statement.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by JoshD75 View Post
    Hmmm, Im not getting errors now but I am getting some crazy results...
    'number' is an int but you are telling scanf it's a float.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Ahh thanks Mike. Got it

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JoshD75 View Post
    Hmmm, Im not getting errors now but I am getting some crazy results...
    First and hardest lesson in programming... "Compiles" does not mean "Works".

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Oh, one more question...

    When I run this while in Visual C++ it works fine, but when I copy the .exe file to my desktop and run it, I type in the "number" and the console just dissapears. Any ideas why?

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Quote Originally Posted by CommonTater View Post
    First and hardest lesson in programming... "Compiles" does not mean "Works".
    very true my friend

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Because it's a console program and the console is only held open long enough to complete the program.

    Open a Command shell ... WIN+R -> type CMD -> click OK .... now CD over to the folder with your program in it, and run it from there... Now the console stays open.

    OR you can modify your code to not exit untill you press a key on the keyboard...

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Quote Originally Posted by CommonTater View Post
    Because it's a console program and the console is only held open long enough to complete the program.

    Open a Command shell ... WIN+R -> type CMD -> click OK .... now CD over to the folder with your program in it, and run it from there... Now the console stays open.

    OR you can modify your code to not exit untill you press a key on the keyboard...
    Thanks Tater,

    If something like this is going to go on multiple computers here at work, which option would you recommend? (I'm gonna make it more elaborate first)

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, first of all if you're going to be installing that on multiple computers... you really need to spike it up with proper error checking, input vetting and range checking. No offense, but what often looks ultra simple, turns out to be far more complicated than beginning programmers seem to realize. Of all the programs I've written (about 90% GIU code) the console code gives me the most trouble.

    If you proceed, I strongly recommend setting it up in a loop so the operator sees something like ...

    "How many boxes in the shipment (0 to exit) : "

    There are some user interface guidelines I've acquired over the years... The big one is... never expect your users to be smart; if a senile Orangutan can't run it without errors, it's too complex.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Quote Originally Posted by CommonTater View Post
    Well, first of all if you're going to be installing that on multiple computers... you really need to spike it up with proper error checking, input vetting and range checking. No offense, but what often looks ultra simple, turns out to be far more complicated than beginning programmers seem to realize. Of all the programs I've written (about 90% GIU code) the console code gives me the most trouble.

    If you proceed, I strongly recommend setting it up in a loop so the operator sees something like ...

    "How many boxes in the shipment (0 to exit) : "

    There are some user interface guidelines I've acquired over the years... The big one is... never expect your users to be smart; if a senile Orangutan can't run it without errors, it's too complex.
    Thanks Tater, I need to clean it up first. As you said it may be simple, but to give it out to others it needs work first..

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... Ask yourself the questions...

    What happens if the end user types their name instead of a number?
    What happens if they don't read the screen and just start pounding away on the keyboard?
    What happens with negative numbers?
    What happens with numbers that are way too big?
    What happens if the operator enters a non-base 10 number like 7A43 ?

    Input is always harder than output... but that doesn't excuse you from making sure the answers given are always correct.

    Also do some googling about the dangers of floating point math on computers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  3. Such a simple problem
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2008, 06:23 AM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM