Thread: Please Help A Noob Here (plz Im Begging You)

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    45

    Please Help A Noob Here (plz Im Begging You)

    hi all
    im really new to c programming so i just need some help from you guys to get me started
    i got to do this task to create a program that will tell the user whether it is a leap yr and how many days there are in the month when enter...
    so far i got this:

    printf("Enter yr and month:");
    scanf("%d", &a);
    if *i got this type just working on it so ignore me*

    {
    printf("has (366) days\n");}
    else{
    printf("has (365) days\n");}

    scanf("%d", &month);
    if (month=1);
    {
    printf("Janurary has 31 days\n");}

    else if (month=2);
    {
    printf("Feburary has 28 days\n");}
    */
    }
    return 0;
    }



    what did i do wrong???


    and later on when im required to change the year like to proper words... how do i do it?

    EG:

    Enter year and month: 1980 2
    nineteen hundred eighty<--------------- how do i do this for years from 1792 - 9999???
    has three hundred and sixty six (366) days
    February has 29 days

    THANK YOU ALL FOR YOUR HELP AND I REALLY APPRECIATE IT

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if (month=1);
    A) Doesn't do what you want. It does:
    Code:
    if ((month = 1) != 0)
    Since 1 is always not equal to zero, what you've achieved is
    Code:
    month =1; if (1) ...
    B) Contains only an empty statement ";" - so nothing actually gets done in the if-statement.

    This in itself will probably lead to the "else if" part further down being an error, because the else doesn't belong to an if-statement.

    --
    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
    Mar 2008
    Posts
    45
    first of all thx a lot for ur reply
    im only been learning c for a couple of days and my teacher is being a jackass
    so how do i fix the if argument if i want my program to acknowledge that the 1 being entered is actually a month? how do i creat a printf statement for this if statemnet

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want
    Code:
    if(month == 1) ...
    There should never be a semicolon DIRECTLY after the parenthesis of an if-statement, as that means that the if-statement contains no code to execute for the "true" choice, which means that there's no need to have an if in the first place. [Unless you have an else after it, in which case the condition should be the opposite to what it is right now].

    By the way, for days per month, a switch-statement is probably more suitable:
    Code:
    switch(month)
    {
       case 4:
       case 6:
       case 9:
       case 11:
          ... 30 days.
          break;
       case 2:
           ... 28 or 29  days. 
           break;
       default:
           if (month < 1 || month > 12)  ... illegal month. ;
           else ... 31 days
           break;
    }
    Only use this if your teacher has introduced "switch" however.


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

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    45
    what other options are there if the bastard hasnt taught switch yet? thx a lot for ur time

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if(month == x) ... 
    else if (month == y) ... 
    ...
    ...
    Of course, you only need to check some of them, like in the switch. If you KNOW that the value is between 1 and 12, then there are 5 months that aren't 31 days: February, April, June, September and October, so those are the only ones you need an if-statement for, the rest are 31 days because that's the only "option left".

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

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    45
    thx for the reply
    how do i line up my if statements and else statements if there are several of them?
    and also what would i need to do if i want this:
    Enter year and month: 1980 2
    nineteen hundred eighty<--------------- how do i do this for years from 1792 - 9999???
    has three hundred and sixty six (366) days
    February has 29 days

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, lets solve one problem at a time. Here is an example of a if/else/if/else chain:
    Code:
    if (x == 1)
        printf("x is 1");
    else if (x == 2)
        printf("x is 2");
    else 
        printf("x is not 1 or 2");
    I'm not going to write the code for you, because you are not learning anything from that.

    As to making "number into text", it's not terribly complicated, but you post something that starts us off, and we'll help you along - that's the "Homework policy" here on Cprogramming.com...

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

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    45
    THIS is what i got so far... is it right?

    #include <stdio.h>

    int main()

    {
    int year, month;

    printf("Enter year and month:");
    scanf("&#37;d", &year);
    if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&& (year%4==0)))
    {
    printf("has three hundred and sixty six (366) days\n");}
    else{
    printf("has three hundred and sixty five (365) days\n");}

    scanf("%d", &month);
    if (month==1);
    printf("Janurary has 31 days");}
    else if (month==2)
    printf("Feburary has 28 days");}
    else (month==3)
    printf("March has 30 days");}

    return 0;
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does it compile?

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

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not put ending brackets on the same line as other code.
    Do use code tags when posting code.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int year, month;
    
    	printf("Enter year and month:");
    	scanf("&#37;d", &year);
    	if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&& (year%4==0)))
    	{
    		printf("has three hundred and sixty six (366) days\n");
    	}
    	else
    	{
    		printf("has three hundred and sixty five (365) days\n");
    	}
    
    	scanf("%d", &month);
    	if (month==1);
    	printf("Janurary has 31 days");
    }
    else if (month==2)
    printf("Feburary has 28 days");
    }
    else (month==3)
    printf("March has 30 days");
    }
    
    return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Quote Originally Posted by Elysia View Post
    Do not put ending brackets on the same line as other code.
    Do use code tags when posting code.
    Actually that's not really needed. Out of all the IDE's I've used Visual Studio's auto-formatting just forces you to do that, but putting them at the end of an if statement or onto the next line is just a matter of style.

    Quote Originally Posted by bobbie18 View Post
    THIS is what i got so far... is it right?

    Code:
    #include <stdio.h>
    
    int main()
    
     {
        int year, month;
    
        printf("Enter year and month:");
        scanf("%d", &year);
        if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&& (year%4==0)))
    {
            printf("has three hundred and sixty six (366) days\n");}
        else{
             printf("has three hundred and sixty five (365) days\n");}
    
        scanf("%d", &month);
        if (month==1);
            printf("Janurary has 31 days");}
        else if (month==2)
        printf("Feburary has 28 days");}
        else (month==3)
        printf("March has 30 days");}
    
        return 0;
    }
    I was under the impression that March always had 31 days.

  13. #13
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Slight improvement on using enumerations as this makes the code slightly self defining.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int year, month;
            enum months{January = 1, February, March};
    
    	printf("Enter year and month:");
    	scanf("%d %d", &year, &month);
    	if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&& (year%4==0)))
    	{
    		printf("has three hundred and sixty six (366) days\n");
    	}
    	else
    	{
    		printf("has three hundred and sixty five (365) days\n");
    	}
    
       if (month == January)
         printf("Janurary has 31 days");
       else if (month == February)
         printf("February has 28 days");
       else if (month == March)
         printf("March has 30 days");
    
    return 0;
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jake123 View Post
    Actually that's not really needed. Out of all the IDE's I've used Visual Studio's auto-formatting just forces you to do that...
    Which is a good thing. But other IDEs are not so nice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jake123 View Post
    Actually that's not really needed. Out of all the IDE's I've used Visual Studio's auto-formatting just forces you to do that, but putting them at the end of an if statement or onto the next line is just a matter of style.
    You can write code any way you like from the COMPILERS standpoint. Elysia's comments are intended to make the code more readable, and for a novice that has little experience, the "it's just a matter of style" is probably not helpful. Giving strict, simple, guidance helps a whole lot more. [Shame I don't always follow my own advice].

    I was under the impression that March always had 31 days.
    So was I.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. The ultimate noob lol...plz help
    By thatpkrguy in forum C++ Programming
    Replies: 14
    Last Post: 11-10-2005, 06:53 PM
  3. plz help here, noob actually .D
    By BjoRn3n in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2005, 03:04 PM
  4. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM
  5. noob: compiling error... plz help!
    By chosii in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 05:53 AM