4 Replies - 827 Views - Last Post: 26 June 2012 - 04:21 AM Rate Topic: -----

#1 CellulaS  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-June 12

Confuse using Switch with if satement

Posted 26 June 2012 - 04:00 AM

Hello guys can you help me im very confusing using switch with if statement inside the switch and with condition tell me was wrong its my first time using if and with switch so please forgive me this the question

sales commission
<1000-5%
3000-5000-10%
1000-2000-20%
< 500
create a program that will calculate the sales commission using switch statement?
#include<iostream.h>
#include<conio.h>
#include<math.h>

int main(){
	

	

   
    char choice;
    double comission;
    double sales;
    
    cout<<"Welcome\n";
    cout<<"Please Choose Option Below\n";
    cout<<"1: Please  Input If Sales are Less than 1000\n";
    cout<<"2: Please Input If Sales are from 3000-5000\n";
    cout<<"3: Please Sales are from 1000-2000\n";
    cout<<"4: Please Sales are less than 500 \n";
    cout<<"n";cin>>choice;
    
	switch (choice)
		{
		case '1':
			cout<<"Please Enter Sales Less Than 1000:";
			cin>>sales;
			if(sales<1000){
				comission = sales * 0.05;
				cout << comission << endl << endl;
			}
			break;
		case '2':
			cout<<"Please Input If Sales are from 3000-5000:";
			cin>>sales;
			
			if(sales =3000 && sales<5000){
				
			comission = sales * 0.1;
				cout << comission << endl << endl;
			}
			break;
		case '3':
			cout<<"Please Input If Sales are from 1000-2000:";
			cin>>sales;
			
			if(sales = 1000 && sales<2000){
				
			comission = sales * 0.2;
				cout << comission << endl << endl;
			}
		
			break;
		case '4':
			cout<<"Please Input If Sales are from 1000-2000:";
			cin>>sales;
			
			if(sales<500){
				
			comission = sales * 0.01;
				cout << comission << endl << endl;
			}
			break;
		default:
			cout << "Invalid input" << endl;
		}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Confuse using Switch with if satement

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1657
  • View blog
  • Posts: 3,135
  • Joined: 30-May 10

Re: Confuse using Switch with if satement

Posted 26 June 2012 - 04:03 AM

> if(sales = 1000 && sales<2000)
You should watch your use of = where you should be using ==
Was This Post Helpful? 1
  • +
  • -

#3 CellulaS  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-June 12

Re: Confuse using Switch with if satement

Posted 26 June 2012 - 04:17 AM

View PostSalem_c, on 26 June 2012 - 04:03 AM, said:

> if(sales = 1000 && sales<2000)
You should watch your use of = where you should be using ==




Thanks Salem_c Now Im Truly Sorry But im Still New At using switch and with if inside it and with condition statement im truly sorry but i can only display <1000 and <500 but not sales>3000 && sales<5000 sales>1000 && sales<2000.. Truly Sorry but im still new at this condition with switch and inside it is an if and still confuse using condition statement

i was wrong with my code i cant let it display 3000 and 1000 sales? im at lost

#include<iostream.h>
#include<conio.h>
#include<math.h>

int main(){
	

	

   
    char choice;
    double comission;
    double sales;
    
    cout<<"Welcome\n";
    cout<<"Please Choose Option Below\n";
    cout<<"1: Please  Input If Sales are Less than 1000\n";
    cout<<"2: Please Input If Sales are from 3000-5000\n";
    cout<<"3: Please Sales are from 1000-2000\n";
    cout<<"4: Please Sales are less than 500 \n";
    cout<<"";cin>>choice;
    
	switch (choice)
		{
		case '1':
			cout<<"Please Enter Sales Less Than 1000:";
			cin>>sales;
			if(sales<1000){
				comission = sales * 0.05;
				cout << comission << endl << endl;
			}
			break;
		case '2':
			cout<<"Please Input If Sales are from 3000-5000:";
			cin>>sales;
			
			if(sales>3000 && sales<5000){
				
			comission = sales * 0.1;
				cout << comission << endl << endl;
			}
			break;
		case '3':
			cout<<"Please Input If Sales are from 1000-2000:";
			cin>>sales;
			
			if(sales>1000 && sales<2000){
				
			comission = sales * 0.2;
				cout << comission << endl << endl;
			}
		
			break;
		case '4':
			cout<<"Please Input If Sales are from 500:";
			cin>>sales;
			
			if(sales<500){
				
			comission = sales * 0.01;
				cout << comission << endl << endl;
			}
			break;
		default:
			cout << "Invalid input" << endl;
		}
}

This post has been edited by GunnerInc: 26 June 2012 - 04:25 AM
Reason for edit:: Removed unneeded quote

Was This Post Helpful? 0
  • +
  • -

#4 busyme12srv  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 18-June 12

Re: Confuse using Switch with if satement

Posted 26 June 2012 - 04:17 AM

(A)

Your if statements are incorrect:

if(sales = 1000 && sales<2000)

It should be if(sales >= 1000 && sales<= 2000)
Similarly it should be :

if (sales >=3000 && sales <= 5000)


(B) The other suggestion is donot use if stmt at all since you are using switch stmt.
int choice;
float sales;


c
out<<"Welcome\n";  

cout<<"Please Choose Option Below\n";  

cout<<"1: Please  Input If Sales are Less than 1000\n";  

cout<<"2: Please Input If Sales are from 3000-5000\n";  

     cout<<"3: Please Sales are from 1000-2000\n";  
     cout<<"4: Please Sales are less than 500 \n";  

cout<<"n"; 


cin << choice;

cin << sales;

switch(choice)
{

case 1:
{

commision = sales * .05);
break;
}
case 2 :
{

commission = sales * .1  ;
break;
}

.
.
.

}



MOD EDIT: Please use code tags when posting code.

:code:

This post has been edited by jimblumberg: 26 June 2012 - 07:54 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 1
  • +
  • -

#5 CellulaS  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 19-June 12

Re: Confuse using Switch with if satement

Posted 26 June 2012 - 04:21 AM

Thanks so much guys that was very helpful much love thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1