need help..quickly

This is a discussion on need help..quickly within the C Programming forums, part of the General Programming Boards category; A program that will accept user input: a the day of the wk (eg tuesday) b an 'n' number of ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    need help..quickly

    A program that will accept user input:
    a the day of the wk (eg tuesday)
    b an 'n' number of days from now, between 1000 and 10000

    The program should output the day of the wk after 'n' days
    input: monday output: thursday

    BUT....

    really simple
    NO
    - functions
    - loops

    only IF's and strings and the % (mod) operator

    to compare use strcmp(string1, string2)

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    6
    Can you make an attempt at getting this problem started and then post your efforts here?

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    I kinda know what the program needs...I just can't seem to get the ifs and math into it

    <code>
    int n;
    char days[]="monday", "tuesday"...etc.
    char day[];
    char d[];

    printf("Enter the day today: ");
    scanf( );
    printf("Enter the number between 1000 and 10000: ");
    scanf( );

    strcmp(days, day);

    amt = n % 7
    if (
    ?
    ?
    ?
    ?

    printf("The day will be%s\n",d);
    </code>

    thats all I got
    Last edited by daisy; 05-22-2003 at 08:46 PM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    I'd appreciate any help at all...Im a beginner at this and Im making it harder than it has to be, it doesn't help with deadlines.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    Daisy

    Ignour people that are rude to you thinking you want to rely only on other people to do your work for you. You need people to help you then ask. If I was good enough I would write it out for you. I failed a course just cause no one would help on one solution. It was only a part time evening course as well, not like it woudl ever get me anything.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    Thank you. I don't want to rely on anyone. And since I did write out what I kinda thought the code would be no one has helped anyways. Im in college for computer programming/analyst as a mature student. What about you?

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    how about something on the lines of this
    correct me if im wrong, just giving it a shot.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    	char *days[] = { "monday",  "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
    
    	int num;
    	int newday;
    	char day[10];
    
    	printf( "Enter day of week and number of days between 1000-10000:\n" );
    	scanf( "%s %d", &day, &num );
    
    	if ( num < 1000 || num > 10000 ) {
    	printf( "Invalid number of days\n" );
    	exit(EXIT_SUCCESS);
    	}
    
    	newday=num%7;
    	printf( "newday: %d\n", newday );
    	if ( strcmp( day, days[0] ) == 0 ) {
    		printf( "output: %s\n", days[newday] );
    	}
    	else if ( strcmp( day, days[1] ) == 0 ) {
    		printf( "output: %s\n", days[newday] );
    	}
    	else if ( strcmp( day, days[2] ) == 0 ) {
    		printf( "output: %s\n", days[newday] );
    	}
    	else if ( strcmp( day, days[3] ) == 0 ) {
    		printf( "output: %s\n", days[newday] );
    	}
    	else if ( strcmp( day, days[4] ) == 0 ) {
    		printf( "output: %s\n", days[newday] );
    	}
    	else if ( strcmp( day, days[5] ) == 0 ) {
    		printf( "output: %s\n", days[newday] );
    	}
    	else if ( strcmp( day, days[6] ) == 0 ) {
    		printf( "output: %s\n", days[newday] );
    	}
    	else {
    		printf( "not valid input day\n" );
    		exit(EXIT_SUCCESS);
    	}
    
    	return 0;
    }
    Last edited by kurz7; 05-23-2003 at 06:02 PM.
    there are only 10 people in the world, those who know binary and those who dont

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    check if output is correct

    added newday to check the value of the mod
    so delete it once u finish checking i guess.

    HOPE THAT HELPS

    its probably not perfect but you can fix up the bugs or the awkward answers.

    might be mod 6 cause i started at 0
    but i think u can edit the *days[] and throw in some crap day at the zero position and just start at days[1]
    Last edited by kurz7; 05-23-2003 at 06:07 PM.
    there are only 10 people in the world, those who know binary and those who dont

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    Question

    these are the variables (simple):

    int dayasint;
    int remainderdays;
    int numberofdaysfromnow;
    char day[10];

    from the code above from kurz7 I got it to work but anytime it's 0 it comes out monday, 1 comes out tuesday ... no matter what day you input.

    do you say:

    if (strcmp(day,"monday")
    {
    monday = 0; tuesday = 1; wednesday = 2;
    //etc...through all the if-days???

    And how would you write that?

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    A Step by step solution -- for learning, I'd recommend starting from scratch but using kurz7's code for a few key features:

    1) define the day-of-week array
    2) input the day-of-week
    3) using if statements, compare the input with the seven values in the array (here of course a loop would be VERY useful) Save the index in 'dow'
    4) input your number
    5) add 'dow' to it
    6) take the modulo-7 (%) of the value -- this value will be the day index for the new day
    7) output from the day-of-week array

    That should do it.

    Walt
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    first of all....

    no loops

    no weekday string only:
    int dayasint;

    I've already used kurz7 program and it has no errors but doesn't work properly with the weekday string.

    this is the first assignment for the year. The teacher hasn't gone through much of anything other than ifs, and strings. and I have a list of the variables the teacher used...above.
    Last edited by daisy; 05-25-2003 at 03:56 PM.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    okay got the code, teacher helped, but when I input any day and number it still comes out...The day is a Monday...all the time. I traced through it and it comes to the first IF and when it outputs Monday it goes straight to the end.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
    
         int intDay;
         int num;
         int newday;
         char day[10];
    
         clrscr();
    
         printf("Enter a day of the week and number of days                 between 1000 and 10000:\n");
         scanf("%s%d",&day,&num);
    
         newday = num % 7;
    
         printf("newday: %d\n",newday);
    
         if (strcmp(day,"monday"))
         {
              intday = 0;
              printf("The day is a Monday");
         }
    
         else if (strcmp(day,"tuesday"))
         {
              intday = 1;
              printf("The day is a Tuesday");
         }
    
         else if (strcmp(day,"wednesday"))
         {
              intday = 2;
              printf("The day is a Wednesday");
         }
    
         else if (strcmp(day,"thursday"))
         {
              intday = 3;
              printf("The day is a Thursday");
         }
    
         else if (strcmp(day,"friday"))
         {
              intday = 4;
              printf("The day is a Friday");
         }
    
         else if (strcmp(day,"saturday"))
         {
              intday = 5;
              printf("The day is a Saturday");
         }
    
         else if (strcmp(day,"sunday"))
         {
              intday = 6;
              printf("The day is a Sunday");
         }
    
         else
         {
              printf("Incorrect Input");
         }
    
         return 0;
    }
    Last edited by digy; 05-26-2003 at 11:13 AM.

  13. #13
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by digy

    Code:
        scanf("%s%d", day, &num);
    
             
         try using stricmp
    
        if(stricmp(day, "monday") == 0)
            puts("use fgets for strings");
    I didn't run your code, so there might be something causing the default behavior.

    HTH
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    It looks to me that your scanf is not working.

    scanf("%s%d",&day,&num);

    for me resulted in junk in both day and num.

    Try adding a printf after the scanf to see what is being entered:

    printf("day=[%s] num=[%d] \n", day, num);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    nope all is working....I put checkups in to see what the computer says I inputted. I still need help on this.
    remember:
    very simple (no loops, functions)
    the code I am working with is the code I wrote a few replys up.

    except for a change......
    if (strcmp(day,"monday")==0)
    intday = 1;

    that's all folks....

    can't figure out what to do with intday = # and to output the answer....

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quickly learn C# for a C++ expert
    By BigDaddyDrew in forum C# Programming
    Replies: 9
    Last Post: 06-06-2004, 04:38 PM
  2. How can I initiate my growth of beard more quickly?
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 07-21-2003, 05:15 PM
  3. console.. character.. screen.. quickly!
    By CodeMonkey in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2003, 06:00 PM
  4. I need excuses for school dance! -- quickly!
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 01-21-2003, 03:42 AM
  5. Reading from a file quickly
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-11-2002, 01:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21