Menu

Program is printing long numbers

2008-09-30
2012-09-26
  • Jenny Tmouh

    Jenny Tmouh - 2008-09-30

    I am writing a program for my C class that asks a user to input a month number and a day number. It determines if the number is within the parameters of 1-12 for months, and 1-31 for days.

    I am using Dev C++ 4.9.9.2.
    Here is my source code

    / Lab 04
    Jenny Tmouh
    COSC 1336
    Due September 29, 2006
    /

    / This program asks the user
    to enter a number for a
    month between 1 and 12 and
    a day between 1 and 31. If
    the numbers do not fall in
    the parameters, it will
    display an error message.
    If the user enters correct
    numbers, the program will
    print them back out.
    /

    include <stdio.h>

    include <stdlib.h>

    main()
    {
    float month;
    float day;

      printf (&quot;Enter a month (use 1 for January, etc.):&quot;);
      scanf (&quot;%d&quot;, &amp;month);
      printf (&quot;Enter a day of the month:\n&quot;);
      scanf (&quot;%d&quot;, &amp;day);
    
      if (month &gt;12)
      printf (&quot;\nYou have entered an incorrect month.\n&quot;);
      else;
      printf (&quot;The month is %d.\n&quot;);
    
      if (day &gt;31)
      printf (&quot;You have entered an incorrect day.\n&quot;);
      else;
      printf (&quot;The day is %d.\n&quot;);
    
      printf (&quot;End of Program\n&quot;);
      system (&quot;pause&quot;);
    

    }

    When I run this program, it displays the month and day numbers as 2293616, instead of the numbers I entered.
    Could someone tell me where my problem is? I have tried switching float with int, and I have also tried switching the %d with %f in all of the statements it shows up in.
    Thank you for your help!

     
    • cpns

      cpns - 2008-09-30

      Engage brain before posting!

      Here:

      > printf ("The month is %d.\n");

      you tell the printf() function to expect an integer argument but provide no argument at all! It is just taking whatever happens to be on the stack at the expected location.

      I bet you wished you had looked at your own code more closely before posting! ;-)

      printf ("The month is %d.\n", month );

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.