Need help else, else if statments switch case

  • Thread starter jekkler
  • Start date
  • Tags
    Switch
In summary: This statement is only reached if all the previous statements are false, so it will only be reached if there are more than 25,000 tickets sold. This means that the bonus will always be 25000.In summary, the bonus contribution is always 25000, regardless of the number of tickets sold.
  • #1
jekkler
1
0
need help else, else if statements... switch case..

need help on the bottom where tryin to define bonusContribution

Code:
// Marcus Molineaux csit 802 assignment 3 t th 645


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
	int tiksold;
	double newadmpt, mondist, revgen,amnovrhd,monraised,admpct;
	double bonusContribution;
	string charname;
	const double tikprice=5.00;
	cout<<"How many tickets were sold? "<<endl;
	cin>>tiksold;
	cout<<"What percent goes to administrative costs (type in whole number)? "<<endl;
	cin>>admpct;
	newadmpt=admpct/100;
		cout<<"How much prize money was distributed? "<<endl;
	cin>>mondist;
	cout<<"What is the name of the charity? "<<endl;
	cin.ignore();
		getline (cin,charname);
revgen=tiksold*tikprice;
amnovrhd=newadmpt*revgen;
		
monraised=(revgen-amnovrhd)-mondist;
		cout << fixed << showpoint << setprecision(2);
        cout<<"Charity:                               "<<setw(12)<<charname<<endl;
		cout<<"Revenue generated:                    $"<<setw(12)<<revgen<<endl;
		cout<<"Administrative overhead:              $"<<setw(12)<<amnovrhd<<endl;
		cout<<"Deducted prize money:                 $"<<setw(12)<<mondist<<endl;
		cout<<"Money raise for charitable fund:      $"<<setw(12)<<monraised<<endl;
		
		
	if (tiksold>10000) bonusContribution=0;
	
	else if ((tiksold<10000) && (tiksold<25000))bonusContribution=3000;
	
	else if (tiksold<24999 && tiksold>50000)bonusContribution=8000;
	
	else if (tiksold<49999 && tiksold>100000)bonusContribution=15000;
	
    else if (tiksold<99999)bonusContribution=25000;
	
	
		cout<<"Bonus Contribution                    $"<<setw(12)<<bonusContribution;
	
	
	
	return 0;
}
 
Physics news on Phys.org
  • #2


Uh... what, specifically, is your problem?
 
  • #3


Your inequalities are going every which way and don't make sense.

Code:
if (tiksold>10000) bonusContribution=0;   // 1
else if ((tiksold<10000) && (tiksold<25000))bonusContribution=3000;  // 2
else if (tiksold<24999 && tiksold>50000)bonusContribution=8000;  //3
else if (tiksold<49999 && tiksold>100000)bonusContribution=15000;  //4
else if (tiksold<99999)bonusContribution=25000; // 5
cout<<"Bonus Contribution                    $"<<setw(12)<<bonusContribution;
Statement 1 says that if there are more than 10,000 tickets sold, there is no bonus.
Statement 2 says that if there are less than 10,000 tickets sold AND less than 25,000 tickets sold, the bonus is 3000. This is equivalent to saying that if fewer than 10,000 tickets are sold, the bonus is 3000.

From these two statements, if there are no tickets sold, the bonus is 3000. If there are 5000 tickets sold, the bonus is 3000. If there are 9999 tickets sold, the bonus is 3000, but if one more ticket is sold, there's no bonus. I'm almost certain that this logic is faulty.

Statement 3 says that if the number of tickets sold is less than 24,999 AND larger than 50,000, the bonus is 8000. There are no numbers that are simultaneously less than 24,999 and larger than 50,000, so there will never be a bonus of 8000 paid.

Statement 4 has a similar problem.

Statement 5 says that if there are less than 99,999 tickets sold, the bonus is 25000.
 

Related to Need help else, else if statments switch case

1. What is the difference between "else" and "else if" statements?

In programming, an "else" statement is used to define a block of code that will be executed if the preceding if statement evaluates to false. On the other hand, an "else if" statement allows for multiple conditions to be checked in a sequence, with each condition being evaluated only if the preceding condition(s) evaluate to false.

2. How do "switch" statements work?

A "switch" statement is used to perform different actions based on different conditions. It evaluates an expression and compares it to multiple cases, and then executes the code associated with the first case that matches the expression.

3. Can "else if" statements and "switch" statements be used interchangeably?

No, "else if" statements and "switch" statements serve different purposes and should not be used interchangeably. "Else if" statements are used to check multiple conditions in sequence, while "switch" statements are used to check a single expression against multiple cases.

4. When should I use "else" statements vs "else if" statements?

"Else" statements should be used when there is only one condition that needs to be checked after an "if" statement. "Else if" statements should be used when there are multiple conditions that need to be checked in sequence after an "if" statement.

5. Are "else" statements required in an "if-else" statement?

No, "else" statements are not required in an "if-else" statement. However, they can be useful for handling cases where none of the conditions in the "if" statement evaluate to true.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Differential Equations
Replies
2
Views
7K
Back
Top