Thread: Help with finding alternative to && operator

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    Question Help with finding alternative to && operator

    Ok , i got an assignment to collage to create a simple program that calculates salery and taxes you pay on each step of the salery, i did it and it was quite good (atleast that the instructor said),
    Now he tells me to do the same but without using the && operator anywhere in the 1st case... after few hours of trying to manuver i gave up, any ideas guys?
    i added my source code below thanks in advance.

    ps. ignore the empty 2nd case its for another part that i already made.
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <ctype.h>
    main()
    {
    int l=1;
    char m=0,car=0;
    unsigned int income,bonus,net_income,gross_income;
    
     while(l)
     {
     printf("\nPress 1 for qu. 1:\nPress 2 for qu. 2:\nPress E or e to Exit:\n");
     fflush(stdin);
     scanf("%c",&m);
     switch(m)//loop so the menu will keep going unless exit is selected
     {
    
     case '1'://first case
      printf("Hello");
      printf("\nPlease enter your bruto salery:");
      scanf("%d",&income);
      printf("\nPlease enter your bonus:");
      scanf("%d",&bonus);
      gross_income=income+bonus;
      if(gross_income<2000)//First salery step
      {
      net_income=gross_income;
      printf("\nYour net worth is: %d",net_income);
      printf("\nCar is not approved!\n");
      }
      if(gross_income>=2000&&gross_income<=4000)//second salery step
      {
      net_income=gross_income;
      printf("Your net worth is :%d",net_income);
      printf("\nCar is not approved!\n");
      }
      if(gross_income>4000&&gross_income<=7000 )//third salery step(there no need for a check if there is a car or not becouse even if he makes 7000 bruto with bonus , he still makes less becouse of the tax
      {
      net_income=gross_income-((gross_income-4000)/5);
      printf("Your net worth is :%d",net_income);
      printf("\nCar is not approved!\n");
      }
      if(gross_income>7000&&gross_income<=10000)//forth salery step
      {
      net_income=gross_income-((gross_income-4000)/5);
      net_income=net_income-(bonus-(bonus/10));
      printf("Do you posses a company owned car press Y/N?");
      fflush(stdin);
      scanf("%C",&car);
      if(car=='y'||car=='Y')//Inner case to check if he holds a car
      {
       net_income=net_income-2000;
       printf("\n2000 nis excluded as car holding fee!\n");
      }
      printf("Your net worth is :%d",net_income);
      }
      break;
     case '2':
      printf("Hello");
      break;
     case 'e':
      printf("Hello");
      printf("\nGoodbye and have a nice day!");
      l=0;
      break;
     default:
      printf("Unknown Char please choose 1,2,E or e");
     }
     }

  2. #2
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    if(gross_income>=2000)
        if(gross_income<=4000)//second salery step
    Something like this?

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    Hmm looks great , so you mean instead the &&, just create 2 cases one inside the other?


    After a quick check it works perfectly and without any && operator ,

    Thanks you!
    Last edited by Redpaw; 07-21-2010 at 09:16 PM.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    No need for nested if.

    Just add else's between the if's.

    If "gross_income < 2000" is not true, "gross_income >= 2000" must be true. No need to check again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You and your instructor should read this, regarding the brain-damage that is fflush(stdin)
    Cprogramming.com FAQ > Why fflush(stdin) is wrong
    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.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Is your instructor asking you to do this as an additional challenge, or is he/she asking because you've gone beyond what you were "supposed" to do at this point? In other words, have you been officially taught about the && operator yet?

    Because I hate instructors that do the "your solution uses things you shouldn't know yet" garbage. What the hell sort of purveyor of knowledge tells people they know things they shouldn't know?

    But maybe I'm jumping to conclusions.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by brewbuck View Post
    Is your instructor asking you to do this as an additional challenge, or is he/she asking because you've gone beyond what you were "supposed" to do at this point? In other words, have you been officially taught about the && operator yet?

    Because I hate instructors that do the "your solution uses things you shouldn't know yet" garbage. What the hell sort of purveyor of knowledge tells people they know things they shouldn't know?

    But maybe I'm jumping to conclusions.
    It was immediately clear to me from looking at the code what the instructor meant. cyberfish has got it too. It's the sillyness of testing the same thing multiple times instead of using an else-if.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by brewbuck View Post
    Because I hate instructors that do the "your solution uses things you shouldn't know yet" garbage. What the hell sort of purveyor of knowledge tells people they know things they shouldn't know?
    While I don't think this is the case here, I share your loathing to that. Maybe because I've lived it: I knew C++ better than the instructors that taught me (and who told me I forgot "return 0;" at the end of main - and yes, this was C++).
    I think it's because of the lack of ability of professors to figure out good problems that require a specific solution...

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you think big value to little value, on these kinds of things, the logic is much simpler:

    if salary>50000

    else if salary > 40000

    else if salary > 30000

    else if salary > 20000

    else //salary > 10000 not needed

    no nesting or other comparisons are needed. Salary's needing to be separated, will always "fall" into the right "bin".

    Switch () can work the same way, working from big to little values.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But there are other glaring problems in your code. Most problematic of all is the indentation, or lack thereof. And main doesn't have a return type, which it should.
    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.

  11. #11
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Yeah, you really need to indent. I don't see how your professor accepted that.

    Math class would say to do it like this, but I don't know what would happen and I don't want to test it.
    Code:
    if(2000<=gross_income<=4000)//second salery step
      {
      net_income=gross_income;
      printf("Your net worth is :%d",net_income);
      printf("\nCar is not approved!\n");
      }

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It doesn't work. Think about it.
    The expression can be written as (2000<=gross_income)<=4000.
    So 2000<=gross_income is evaluated first and becomes a boolean expression. That is, 1 or 0.
    So then the rest becomes 1<=4000 or 0<=4000. Doesn't make sense, does it?
    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.

  13. #13
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    If I did the cast right, this will work.
    Code:
    if((unsigned int)gross_income - 2000 <= 2000)//second salery step
      {
      net_income=gross_income;
      printf("Your net worth is :%d",net_income);
      printf("\nCar is not approved!\n");
      }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    True. That might work.
    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
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Code:
    if((gross_income - 2000) == (gross_income - 2000 & ~2000))//second salery step
      {
      net_income=gross_income;
      printf("Your net worth is :%d",net_income);
      printf("\nCar is not approved!\n");
      }
    How's that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some Interview questions, please help
    By higaea in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2010, 06:35 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. friendly assignment operator
    By Aidman in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2007, 01:09 AM
  4. int vs BIG INT
    By rumi_red in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 04:15 AM
  5. Overriding = operator.
    By Strahan in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2001, 03:26 PM