Thread: If statements not working

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    12

    Question If statements not working

    Hi all.. I've got a problem with some code I'm writing and I don't know how to make it do what I want. Every time I debug it, it gives me a 0 value. The goal is to write a program that given the date as input, it returns the day number. I'm sure you've all seen this somewhere before.

    Code:
    // day-date.cpp : Defines the entry point for the console application.
    //
    
    #include <stdio.h>
    #include <stdafx.h>
    #include <conio.h>
    #include <math.h>
    
    
    int
    main ()
    
    {
       int number;
       double x,y;
       
       
    
       printf("Enter month \n");
       scanf("%d",&x);
       
       printf("Enter day \n");
       scanf("%d", &y);
       {
    	   if (x == 1){
                number = y;
    	   }
    	   else if (x == 2){
                number = 31 + y;
    	   }
    	   else if (x == 3){
                number = 31 + 28 + y;
    	   }
    	   else if (x == 4){
                number = 31 + 28 + 31 + y;
    	   }
    	   else if (x == 5){
                number = 31 + 28 + 31 + 30 + y;
    	   }
    	   else if (x == 6){
                number = 31 + 28 + 31 + 30 + 31 + y;
    	   }
    	   else if (x == 7){
                number = 31 + 28 + 31 + 30 + 31 + 30 + y;
    	   }
    	   else if (x == 8){
                number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + y;
    	   }
    	   else if (x == 9){
                number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + y;
    	   }
    	   else if (x == 10){
                number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + y;
    	   }
    	   else if (x == 11){
                number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + y;
    	   }
    	   else if (x == 12){
                number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + y;
    	   }
           else 
    			printf("invalid");
       }
       printf("The number for the date is %lf", &number);
       
     
    
       getch();
    
       return(number);
    
    }
    Any help is appreciated.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect you do not want x and y to be "double". If you use printf("x=%f, y=%f\n", x, y); after you've read the values with scanf, I'm pretty sure it will NOT show the value you expect.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    One problem, you declare x and y as doubles but try to read them in as integers. Change them to ints.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Ok. I changed x and y to ints, but still getting 0.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seeing as you print it using
    Code:
    printf("The number for the date is %lf", &number);
    format for "long double" (or undefined format), I'm not entirely surprised it goes wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Can you show me how to fix it?

    I honesly have no idea.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by leroyjenkins View Post
    Can you show me how to fix it?

    I honesly have no idea.
    How do you THINK you should print an integer?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Code:
    printf("The number for the date is %c", &number);

    like so?


    Edit:
    Code:
    printf("The number for the date is %d", &number);
    Tried this and it gave me an oddly huge number.
    Last edited by leroyjenkins; 02-25-2009 at 11:03 AM. Reason: I fail

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    The sooner you memorize the components of a format string, the better.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    I tried this

    Code:
    printf ("%5d\n", The date number is %5d);
    and nothing but a thousand errors.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How did reading about %d lead you to conclude that most of the string should be outside the "" marks?
    printf ("%5d\n", The date number is %5d);

    Your post #8 was nearly there apart from a stray &
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    That's what I read.
    Code:
    printf ( "%5d\n", 123 );      /* Prints "  123" */
    This is what I have in there now.
    Code:
    printf ("%5d\n, The date number is %5d");

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Well i'll be dang. It works now. It was just that last statement.

    Code:
    printf("The number for the date is %d", number);

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Thanks everyone. Thank you all very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. having problems with my if statements
    By bajanstar in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 01:39 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM