Thread: Need help with assignment

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    9

    Exclamation Need help with assignment

    I need help with this program, so basically, it is suppose to ask for information of a flight and produce air mile points accordingly. I can't seem to make this program work correctly. When birth_month == travel_month, the question if the day is saturday or tuesday is not suppose to display, or have a prompt after wards, but it does and the bonus for traveling on a saturday is always included even if I input n.

    Please help, I just started programming and I've been at this for a long time and I can't get it to work and don't know what the problem is...the compiler I am using is on matrix but I'm not sure what that is. I've been told that its similar to borland.

    Code:
    #include <stdio.h>
    
    main () {
    
    	/*Variables are getting initiated*/
    	int km, class, birth_month, travel_month, base_points, bonus_birth = 0, bonus_saturday = 0, bonus_tuesday = 0, tot = 0;
    	char saturday, tuesday;
    
    	printf ("Welcome to Jet Lag Airlines!\n");
    	printf ("Enter the distance travelled in km: ");
    	scanf ("%d", &km); /*Prompts user to input distance travelled in km*/
    	printf ("\nEnter the class of travel (1-Economy, 2-Business, 3-First Class): ");
    	scanf ("%d", &class); /*Prompts user to input the class of their ticket*/
    	/*Checks if the km travelled is less or more than 1000km and which class th*/
    	if (km <= 1000 && class == 1) {
    		base_points = 1000;
    	}
    	else if (km <= 1000 && class == 2) {
    		base_points = 2000;
    	}
    	else if (km <= 1000 && class == 3) {
    		base_points = 3000;
    	}
    	else if (km > 1000 && class == 1) {
    		base_points = km;
    	}
    	else if (km > 1000 && class == 2) {
    		base_points = km * 2;
    	}
    	else {
    	base_points = km * 3;
    	}
    	printf ("\nIn which month were you born (1-Jan, 2-Feb, ..., 12-Dec): ");
    	scanf ("%d", &birth_month);
    	printf ("\nIn which month did travel take place (1-Jan, 2-Feb, ..., 12-Dec): ");
    	scanf ("%d", &travel_month);
    	if (birth_month == travel_month) {
    		bonus_birth = base_points * 2;
    	}
    	while (getchar() != '\n') continue;
    	if (birth_month != travel_month) {
    		printf ("\nWas travel on a Saturday (Y-Yes, N-No): ");
    		scanf ("%c", &saturday);
    	}
    	while (getchar() != '\n') continue;
    	if (saturday == 'n' || 'N' && birth_month != travel_month) {
    		printf ("\nWas travel on a Tuesday (Y-Yes, N-No): ");
    		scanf ("%c", &tuesday);
    	}
    	if (saturday == 'y' || 'Y' && birth_month != travel_month) {
    	bonus_saturday = base_points * 1.5;
    	}
    	else if (tuesday == 'y' || 'Y' && birth_month != travel_month) {
    	bonus_tuesday = base_points;
    	}
    	tot = base_points + bonus_birth + bonus_saturday + bonus_tuesday;
    	printf ("\nSummary of points awarded:\n");
    	printf ("==============================================\n");
    	printf ("Base points: \t\t\t\t%d", base_points);
    	printf ("\nBonus for birth month: \t\t\t%d", bonus_birth);
    	printf ("\nBonus for Saturday travel: \t\t%d", bonus_saturday);
    	printf ("\nBonus for Tuesday travel: \t\t%d", bonus_tuesday);
    	printf ("\n==============================================\n");
    	printf ("Total points: \t\t\t\t%d", tot);
    	if (tot >= 5000) {
    		printf ("\nCongratulations! You have enough points to redeem for a reward.\n");
    	}
    	else {
    		printf("\nNot enough to redeem for a reward.\n");
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    saturday == 'n' || 'N'
    This does not check whether the variable saturday contains one of two possible values. It checks whether saturday is equal to 'n'; if that is false, it checks whether 'N' (not whether saturday is equal to 'N', just whether 'N').

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Quote Originally Posted by tabstop View Post
    Code:
    saturday == 'n' || 'N'
    This does not check whether the variable saturday contains one of two possible values. It checks whether saturday is equal to 'n'; if that is false, it checks whether 'N' (not whether saturday is equal to 'N', just whether 'N').
    I don't really get it...so what do I have to change so that I will not get prompted after birth_month == travel_month? and how do I stop saturday_bonus from always being calculated?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to check whether saturday == 'n' and whether saturday == 'N'.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by PKisMe View Post
    I don't really get it...so what do I have to change so that I will not get prompted after birth_month == travel_month? and how do I stop saturday_bonus from always being calculated?
    You need to be more specific...
    Code:
    if (((saturday == 'n') || (saturday == 'N')) && (birth_month != travel_month))
     ...
    Plus it really doesn't matter if it's a birth_month == travel_month if the travel was not on saturday, so...
    Code:
    if ((saturday == 'n') || (saturday == 'N'))
    ...
    Last edited by CommonTater; 06-14-2011 at 07:40 PM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    Your code is 100% correct.
    Worked for me. Try a different compiler.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    Quote Originally Posted by PKisMe View Post
    and how do I stop saturday_bonus from always being calculated?
    Conditional statements is the way to go. Use if else. If you still don't get it, tell me, I'll help.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    AWESOME! I GOT IT WORKING NOW! So happy !!!! Just have one more issue, after I enter birth_month as the same as travel_month, I still get a prompt afterward before the final output. How can I fix this?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by guitargod View Post
    Your code is 100% correct.
    Worked for me. Try a different compiler.
    Not even close.... C simply does not know how to evaluate if (q == 1 || 2) ... what happens is that it evaluates if (q = 1) and if q = 1 it gets "true" and the if() clause is executed... BUT if q = 2 it gets "false" and continues on to if (2) which is always non-0 or "true" and the if clause will execute... The expression always evaluates to "true"...
    Last edited by CommonTater; 06-14-2011 at 07:54 PM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by PKisMe View Post
    AWESOME! I GOT IT WORKING NOW! So happy !!!! Just have one more issue, after I enter birth_month as the same as travel_month, I still get a prompt afterward before the final output. How can I fix this?
    Fix the logic in your if statements... all of them!

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Quote Originally Posted by CommonTater View Post
    Fix the logic in your if statements... all of them!
    I already fixed them, placed in brackets in all the necessary places. My program works fine now.

    I still have a problem with this part though...

    Code:
    	if (birth_month != travel_month) {
    		printf ("\nWas travel on a Saturday (Y-Yes, N-No): ");
    		scanf ("%c", &saturday);
    	}
    	while (getchar() != '\n') continue;
    	if (((saturday == 'n') || (saturday == 'N')) && (birth_month != travel_month)) {
    		printf ("\nWas travel on a Tuesday (Y-Yes, N-No): ");
    		scanf ("%c", &tuesday);
    	}
    even though I press y for saturday, it still displays "Was travel on a Tuesday (Y-Yes, N-No): " which is not suppose to and I'm not sure why that is...
    Last edited by PKisMe; 06-14-2011 at 07:53 PM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by PKisMe View Post
    I already fixed them, placed in brackets in all the necessary places. My program works fine now, I just want to remove that prompt after asking for travel_month.
    It's not just about brackets... But anyway... if the following prompts are not wanted if birth month = travel month, then they should be inside a conditional statement such that they are executed only when the two are not the same.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    I kind of fixed it, I removed while (getchar() != '\n') continue; and that fixed the problem, but I kind of have a new problem, which I posted before your post.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    9
    Ahh why is : "Was travel on a Tuesday (Y-Yes, N-No): " still displaying if I input y for saturday??

    Code:
    	if (birth_month != travel_month) {
    		printf ("\nWas travel on a Saturday (Y-Yes, N-No): ");
    		scanf ("%c", &saturday);
    	}
    	while (getchar() != '\n') continue;
    	if (((saturday == 'n') || (saturday == 'N')) && (birth_month != travel_month)) {
    		printf ("\nWas travel on a Tuesday (Y-Yes, N-No): ");
    		scanf ("%c", &tuesday);
    	}

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by PKisMe View Post
    I already fixed them, placed in brackets in all the necessary places. My program works fine now.

    I still have a problem with this part though...
    Try it like this...
    Code:
    if (birth_month != travel_month) 
      { printf ("\nWas travel on a Saturday (Y-Yes, N-No): ");
         scanf ("%c", &saturday);
    
         while (getchar() != '\n');
    
         if ((saturday != 'y') && (saturday != 'Y')) 
           { printf ("\nWas travel on a Tuesday (Y-Yes, N-No): ");
              scanf ("%c", &tuesday); }
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Car assignment help
    By anik18 in forum C Programming
    Replies: 4
    Last Post: 05-09-2009, 09:40 PM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. C Assignment help
    By aditya_t90 in forum C Programming
    Replies: 5
    Last Post: 03-28-2009, 10:54 AM
  4. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  5. Help With Assignment Please
    By MegaVortex in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2004, 09:40 PM