Need help else, else if statments switch case

  • Thread starter Thread starter jekkler
  • Start date Start date
  • Tags Tags
    Switch
Click For Summary
SUMMARY

The discussion centers on the logical errors in the implementation of bonus contribution calculations in a C++ program. The code provided contains multiple flawed conditional statements that lead to incorrect bonus assignments based on ticket sales. Specifically, the conditions for bonus contributions are contradictory and do not cover all possible scenarios, resulting in potential miscalculations. The correct approach requires a clear and logical structure for the if-else statements to ensure accurate bonus determination based on the number of tickets sold.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with conditional statements (if, else if)
  • Basic knowledge of logical operators
  • Experience with debugging and code logic analysis
NEXT STEPS
  • Review C++ conditional statements and their proper usage
  • Learn about logical operators and their implications in programming
  • Study debugging techniques for identifying logical errors in code
  • Explore best practices for structuring if-else statements for clarity and accuracy
USEFUL FOR

Programmers, especially those working with C++, students learning programming logic, and anyone involved in developing applications that require conditional calculations.

jekkler
Messages
1
Reaction score
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


Uh... what, specifically, is your problem?
 


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.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
3K