Thread: Simple problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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'

  2. #2
    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.

  3. #3
    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.

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

  5. #5
    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?

  6. #6
    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".

  7. #7
    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

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