if else issues

Why is my if.else failing

Page 1 of 1

4 Replies - 871 Views - Last Post: 09 March 2009 - 07:02 PM Rate Topic: -----

#1 ellevador  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 01-March 09

if else issues

Post icon  Posted 09 March 2009 - 06:49 PM

When I try running this I get errors illegal else without matching if syntax errors. Any help would rock cause I thought it was looking pretty good until that...
#include <iostream>
using namespace std;
int main ()
{
	//declaring variables
	int numPeople = 0;
	int charge = 0;
	int totalPeople = 0;
	int count = 0;
	int totalCharge = 0;
	char reply = 'Y';

	//loop 
	do
	{
	cout<<"Enter the number of registrants ";
	cin>> numPeople;
	count += 1;
	totalPeople += numPeople;
	cout<<"Do you want to enter more registrants? Y or N ";
	cin>>reply;
	}

	if	(totalPeople >= 1 && totalPeople <= 3)
	{	(totalCharge = '150')
		cout<< "The total charge per person is " << totalCharge <<endl;
	}
	else if (totalPeople >= 4 && totalPeople <=9)
		totalCharge = '100';
		cout<< "The total charge per person is " << totalCharge <<endl;
	else if (totalPeople >=10)
		totalCharge = '90';

	//end if
	while (toupper(reply)== 'Y');
	//total charge, total people, and average charge calculations
	cout<< "The total number of registrants are  " << totalPeople <<endl;
	cout << "The total charge is " << totalCharge <<endl;
	return 0; 
}



Is This A Good Question/Topic? 0
  • +

Replies To: if else issues

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: if else issues

Posted 09 March 2009 - 06:57 PM

You have several bracket errors. Your do..while() loop has no opening bracket
	do
	{ <<<< could be opening bracket or just a block start bracket
	cout<<"Enter the number of registrants ";
	cin>> numPeople;
	count += 1;
	totalPeople += numPeople;
	cout<<"Do you want to enter more registrants? Y or N ";
	cin>>reply;
	} <<<<< block end bracket


The else clause is missing two brackets:
	else if (totalPeople >= 4 && totalPeople <=9)   {  <<<<<<<<<<
		totalCharge = '100';
		cout<< "The total charge per person is " << totalCharge <<endl;
	 }   <<<<<<<<<<<<<


Finally, there is no closing bracket before the while() statement;
	//end if
   }   <<<<<<<<<<<<<< here
	while (toupper(reply)== 'Y');


This post has been edited by n8wxs: 09 March 2009 - 07:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: if else issues

Posted 09 March 2009 - 06:58 PM

You sure do have some wonderfully new & inventive & sloppy indenting going on.

Properly written code would have saved you the effort :)

#include <iostream>
using namespace std;
int main(void) {
	//declaring variables
	int numPeople = 0;
	int charge = 0;
	int totalPeople = 0;
	int count = 0;
	int totalCharge = 0;
	char reply = 'Y';

	//loop
	while (toupper(reply)== 'Y') {
	  cout<<"Enter the number of registrants ";
	  cin>> numPeople;
	  count += 1;
	  totalPeople += numPeople;
	  cout<<"Do you want to enter more registrants? Y or N ";
	  cin>>reply;

	  if (totalPeople >= 1 && totalPeople <= 3) {
		//(totalCharge = '150') <-- wtf is this?
		cout << "The total charge per person is " << totalCharge << endl;
	  }
	  else if (totalPeople >= 4 && totalPeople <=9) {
		totalCharge = '100';
		cout<< "The total charge per person is " << totalCharge << endl;
	  }
	  else if (totalPeople >=10) {
		totalCharge = '90';
	  }

	//end if
	}
	//total charge, total people, and average charge calculations
	cout<< "The total number of registrants are  " << totalPeople <<endl;
	cout << "The total charge is " << totalCharge <<endl;

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 ellevador  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 01-March 09

Re: if else issues

Posted 09 March 2009 - 07:01 PM

thanks that fixed it all. mucho appreciation!

Thanks for the laugh too! Most of the time, I have no idea what I am doing!
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: if else issues

Posted 09 March 2009 - 07:02 PM

This is one of my gripes about C/C++... it allows coders to get sloppy, without really knowing any better.

if (variable==value) {
  ...
}



Is the same as
if (variable==value) 
  ...



So the problem comes in when you use an else like the following :

if (variable==value) 
  ...
else if (variable==value) 
  ... // only this line is executed as the if... & in the else...
  ... // this line breaks the if, else, & is no longer in the original if/then/else structure
else if (variable==value) 
  ... // now we get an error, misplaced else



Hope this clears it up :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1