Thread: if else switch ????? which one ?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    25

    if else switch ????? which one ?

    please run this simple program......

    why wont this work ?

    i tried it first with if...else
    Code:
    /*why will this program not run? what did i do wrong with the if else statements/conditions ?*/
    
    
    
    #include <conio.h>
    #include <iostream.h>
    main()
    {
    	double sales, sales_amount, quantity, total_sales, vat;
       total_sales = sales + vat;
       vat = 0.175*sales;
    
       cout << "\nTotal Sales = sales amount * quantity\n";  endl;
       cout << "\nEnter the sales amount\n";
       cin >>  sales_amount;
       cout << "\nEnter the quantity\n" ;
       cin >>  quantity ;
       sales = (sales_amount * quantity);
       cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
       cout << "\n" << "Sales is: \t" << sales << "\n";
       cout << "\ntotal sales inc VAT is: \t" << (sales + 0.175*sales) << "\n";
    
       	if (sales < 25) cout << "\nNo commission";                                         //<-------------why will this not run?
          else if (50 > sales < 25) cout << "\n 5% commission   " << total_sales * 0.05;
          else if (75>sales<51) cout << "\n 10% commission    "; << total_sales * 0.10;
          else if (100>sales<76)cout << "\n 20% commission"; << total_sales * 0.20;
          else cout << "20% commission" ;
       getch();
    
       }
    i tried it with a switch statement

    Code:
    #include <conio.h>
    #include <iostream.h>
    main()
    {
    	double sales, sales_amount, quantity, total_sales, vat;
       total_sales = sales + vat;
       vat = 0.175*sales;
    
       cout << "\nTotal Sales = sales amount * quantity\n";  endl;
       cout << "\nEnter the sales amount\n";
       cin >>  sales_amount;
       cout << "\nEnter the quantity\n" ;
       cin >>  quantity ;
       sales = (sales_amount * quantity);
       cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
       cout << "\n" << "Sales is: \t" << sales << "\n";
       cout << "\ntotal sales inc VAT is: \t" << (sales + 0.175*sales) << "\n";
    
       switch(sales) {
       	case <25:
          	cout << "\nNo commission";
             break;
          case 50 > sales < 25:
             cout << "\n 5% commission   " << total_sales * 0.05;
             break;
          case 75 > sales < 51:
          	cout << "\n 10% commission    "; << total_sales * 0.10;
          case 100 > sales < 76:
          	cout << "\n 20% commission"; << total_sales * 0.20;
          default:
          	cout << "\n 20% commission";
       }
       getch();
    }
    someone help me out please...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can't use this as a condition in an if statement.
    Code:
    if(50 > sales < 25)
    However, you can use
    Code:
    if(sales > 25 && sales < 50)
    where "&&" means "and". ("||" means "or".)

    As for switch statements, the case labels have to be integers, so that's not very helpful.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    Also, this is wrong
    Code:
     	if (sales < 25) cout << "\nNo commission";                                         //<-------------why will this not run?
          else if (50 > sales < 25) cout << "\n 5% commission   " << total_sales * 0.05;
          else if (75>sales<51) cout << "\n 10% commission    "; << total_sales * 0.10;
          else if (100>sales<76)cout << "\n 20% commission"; << total_sales * 0.20;
          else cout << "20% commission" ;

    when you use an if statement, or a for loop, if you do not use brackets, then the only line of code that will be ran is the SINGLE LINE IMMEDIATELY FOLLOWING IT
    for example


    Code:
    if(blar >1 )
          Runsomething();
          Runsomethingelse();
    Runsomethinglese() should not be thought of as part of the if statement. You must use braces. Same rule for for loops, while loops
    Last edited by smasherprog; 05-06-2010 at 11:43 PM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think he meant to put those semicolons in the chained output expressions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Try and read expression such as this
    Code:
    50 > sales < 25
    aloud to yourself.

    You may find yourself wanting to use the word "between", but there is no such operator as "between". You have to read < as "less-than" and > as "greater-than" etc.

    You'll probably also find yourself wanting to use the word "and" in your description, but this does not match what you wrote because there was no && (read as "and") in that expression.
    This actually indicates what the mistake is. You do have two conditions there, so using an && is actually necessary.
    It should read as:
    "sales is greater-than 25 AND sales is less-than 50".
    Why do we have to mention sales twice? Because you have to be able to break up ANDed expressions into individual expressions and "is less than 50" is not valid as nobody would know what you are talking about.

    You cannot use double in a switch.
    Lastly, what happens if sales comes out to exactly 25 or exactly 50?!
    Last edited by iMalc; 05-07-2010 at 02:28 AM.
    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"

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by iMalc View Post
    Try and read expression such as this
    Code:
    50 > sales < 25
    aloud to yourself.

    You may find yourself wanting to use the word "between", but there is no such operator as "between". You have to read < as "less-than" and > as "greater-than" etc.

    You'll probably also find yourself wanting to use the word "and" in your description, but this does not match what you wrote because there was no && (read as "and") in that expression.
    This actually indicates what the mistake is. You do have two conditions there, so using an && is actually necessary.
    It should read as:
    "sales is greater-than 25 AND sales is less-than 50".
    Why do we have to mention sales twice? Because you have to be able to break up ANDed expressions into individual expressions and "is less than 50" is not valid as nobody would know what you are talking about.

    You cannot use double in a switch.
    Lastly, what happens if sales comes out to exactly 25 or exactly 50?!

    i tried tjis and it works better. Thanks


    Code:
    #include <conio.h>
    #include <iostream>
    
    int main()
    {
    	double sales, sales_amount, quantity, total_sales, vat;
    	total_sales = sales + vat;
    	vat = 0.175*sales;
    
    	cout << "\nTotal Sales = sales amount * quantity\n" << endl;
    	cout << "\nEnter the sales amount\n";
    	cin >>  sales_amount;
    	cout << "\nEnter the quantity\n" ;
    	cin >>  quantity ;
    	sales = (sales_amount * quantity);
    	cout << "\n" << sales_amount << "  *  " << quantity << " = " << sales << "\n";
    	cout << "\n" << "Sales is: \t" << sales << "\n";
       total_sales = (sales + 0.175 * sales);
    	cout << "\ntotal sales inc VAT is: \t" << total_sales << "\n";
    
    	if(total_sales<25)
    		cout << "\nNo commission";                                         //<-------------why will this not run?
    	else if(total_sales<50)
    		cout << "\n5% commission of total sales is " << 0.05 * total_sales;
    	else if(total_sales<75)
    		cout << "\n10% commission of total sales is " << 0.10 * total_sales;
    	else if(total_sales>75)
    		cout << "\n20% commission of total sales is " << 0.20 * total_sales;
    	getch();
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM