i need some more C help

Discussion in 'Mac Programming' started by andrewface, Feb 8, 2007.

  1. macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #1
    i need to write a program that asks the user for the number of days of the week and the starting day of the month and i need it to print out a months calendar

    any advice?

    edit: heres what ive got so far

    #include <stdio.h>

    int main() {
    // declare variables
    int numberofdays, startday;
    char nameofmonth[10];
    //Explains program and asks for the name of the month, and week start number
    printf("This program creates a calendar for one month\nPlease enter the name of the month:");
    scanf("%s", nameofmonth);
    printf("Please enter the number of days in the month:");
    scanf("%d", &numberofdays);
    printf("Please enter the day of the week that the month starts; starting with Sunday 1-7:");
    scanf("%d", &startday);
    printf("%17s\nSun Mon Tue Wed Thu Fri Sat\n", nameofmonth);
    printf("1 2 3 4 5 6 7"); //lost here this doesnt work...

    return 0;

    }
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    You'd probably want to use the tab 't character to separate things to make them a bit neater.

    Also, you'll likely want to use a calculation to figure out how many weeks to loop. You know how to use for loops, right?

    The modulo operator % should come in handy, too.
     
  3. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #3
    yes i have no idea what calculations to use in the for loops thats the thing
     
  4. macrumors 6502

    Joined:
    Oct 24, 2005
    #4
    I don't understand what your program is suppose to do. Could you explain it a little bit clearer?

    All your program does is takes the input. Not to be rude but a monkey could have written that code. You could at least try to solve the problem yourself instead of asking people to do your homework.
     
  5. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #5
    Perhaps, then, you should sit down with pencil and paper and figure out how you would do it step-by-step. Once you figure out how that works, you can translate it to code.
     
  6. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #6
    im one dumb monkey then
     
  7. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #7
    Andrew, some free advice...

    You have to be able to communicate your thoughts onto paper (or in type) to solve "math" type problems (aka word problems). Once you can do it by math, you can then write code that implements the math.

    Fumbling with code before you have an action plan is a waste of time. Take the time to write out what it is you need to do, as a series of steps, then approach the problem from a coding standpoint.

    - get the month (this goes on the top of the report)
    - get the # of days in the month
    - get the first day of the month.

    You have that so far.

    The first day of the month will be your trigger to starting printing with "1" when you get to it in the first loop. For instance, if the day entered was 4, then for the first 3 positions in the row for the first week, you'll print blanks. Once you hit your target day, then you can start clicking off 1, 2, 3, and so on.

    You need two loops - an outer loop and an inner loop.

    The outer loop will print the line you build in the inner loop and manage the CRLF to create make the report look like a table.

    The inner loop will build the print line with

    1) either blanks and numbers (for the first week), or
    2) all numbers (for the first, middle or last weeks) or
    3) numbers and blanks (for the last week).

    Your trigger to quit the loops is when you've accounted for "numberofdays" days.

    So, keep coding with this in mind and see what you come up with.

    Todd
     
  8. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #8
    thanks alot ima keep going at it
     
  9. macrumors 6502

    Joined:
    Oct 24, 2005
    #9
    my comment was not to insult you. It was actually suppose to help you. I know that you're just starting out but asking for people to do your homework is not going to make you a better programmer. You need to practice get yourself thinking like a programmer. You learn from experience how to tackle types of problems.
     
  10. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    Just an FYI - I worked it out. So, when you're ready with your next hurdle, holler.

    Code:
    [Session started at 2007-02-08 23:01:19 -0600.]
    This program creates a calendar for one month
    Please enter the name of the month: December
    Please enter the number of days in the month: 31
    Please enter the day of the week that the month starts; starting with Sunday 1-7: 7
    
    
    	December
    	Sun 	Mon 	Tue 	Wed 	Thu 	Fri 	Sat
    	    	    	    	    	    	    	  1 
    	  2 	  3 	  4 	  5 	  6 	  7 	  8 
    	  9 	 10 	 11 	 12 	 13 	 14 	 15 
    	 16 	 17 	 18 	 19 	 20 	 21 	 22 
    	 23 	 24 	 25 	 26 	 27 	 28 	 29 
    	 30 	 31 	    	    	    	    	    
    
    Calendar has exited with status 0.   	    	    	    
    
    and...

    Code:
    [Session started at 2007-02-08 23:03:13 -0600.]
    This program creates a calendar for one month
    Please enter the name of the month: February
    Please enter the number of days in the month: 28
    Please enter the day of the week that the month starts; starting with Sunday 1-7: 1
    
    
    	February
    	Sun 	Mon 	Tue 	Wed 	Thu 	Fri 	Sat
    	  1 	  2 	  3 	  4 	  5 	  6 	  7 
    	  8 	  9 	 10 	 11 	 12 	 13 	 14 
    	 15 	 16 	 17 	 18 	 19 	 20 	 21 
    	 22 	 23 	 24 	 25 	 26 	 27 	 28 
    
    Calendar has exited with status 0.
    
    Todd
     
  11. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #11
    Don't you think you've already helped too much? He's supposed to be paying attention in class and reading his books and learning to solve problems on his own, as well as code the solutions.

    Maybe, you should consider waiting until he's got it running fairly well.
     
  12. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #12
    No I don't think I've helped too much, and neither you or I know that he is or is not doing what you say he should be doing. The guy is obviously struggling with basics here. I simply tried to point his head in the right direction.

    Am. I don't categorize showing the output as any kind of give-away. The earlier prose was to get him to think about the overall objectives and thinking (and hopefully heading) in the right direction.

    Todd
     

Share This Page