6 Replies - 34362 Views - Last Post: 25 March 2011 - 08:12 AM Rate Topic: -----

#1 mikex6989   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 109
  • Joined: 18-February 11

Using multiple "else if" statements?

Posted 25 March 2011 - 06:55 AM

*is it wrong to use multiple "else if" statements like the ones that i have? even though the program runs, i just want to double check. thanks!


#include <fstream>
#include <iomanip>
using namespace std;
int main() {

	int i;
	int students;
	int id;
	int credits;
	char stateCode;
	char dormCode;
	float tuition = 0;
	ifstream inData;
	ofstream outData;

	outData << setiosflags(ios::fixed|ios::showpoint|ios::right);
	outData << setprecision(2);

	inData.open("inputHW10.txt");
	outData.open("outputHW10.txt");

	inData >> students;

	outData << setw(10) << "Id number " << setw(13) << "Credits " << setw(15) << "State Code " 
		<< setw(15) << "Dorm Code " << setw(13) << "Tuition " << endl << endl;

	for(i=0; i<students; i++) {

		inData >> id >> credits >> stateCode >> dormCode;

		if ((credits < 12) && (stateCode == '1'))
			tuition = 450*credits;
		
		else if ((credits < 12) && (stateCode == '2'))
			tuition = 800*credits;

		else if ((credits >= 12) && (stateCode == '1'))
			tuition = 6000;

		else if ((credits >= 12) && (stateCode == '2'))
			tuition = 12000;

		if (dormCode == '1')
			tuition = tuition + 4000;
		else
			tuition;

		outData << setw(5) << id << setw(15) << credits << setw(13) << stateCode << setw(15) 
			<< dormCode << setw(10) << "$" << tuition << endl;

	}

	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Using multiple "else if" statements?

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Using multiple "else if" statements?

Posted 25 March 2011 - 07:06 AM

Moved to C/C++ Forum.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5495
  • View blog
  • Posts: 17,070
  • Joined: 25-December 09

Re: Using multiple "else if" statements?

Posted 25 March 2011 - 07:33 AM

It is ok to use multiple if/if else statements. However you should terminate your if else if clauses with an else even if it is blank {/* BLANK */}. This will point out that this is where this clause is to end.

          else if ((credits >= 12) && (stateCode == '1'))
			tuition = 6000;

		else if ((credits >= 12) && (stateCode == '2'))
			tuition = 12000;
                else {cout << "Invalid entry" << endl;}
                    



Also I would recommend that you use braces {} on each of your clauses, even the one liners to reinforce the program flow.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 mikex6989   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 109
  • Joined: 18-February 11

Re: Using multiple "else if" statements?

Posted 25 March 2011 - 07:38 AM

thanks! so u mean put braces for each "if" and "else if"?

for example:

if ((credits < 12) && (stateCode == '1')){
			tuition = 450*credits;}
		
		else if ((credits < 12) && (stateCode == '2')){
			tuition = 800*credits;}

		else if ((credits >= 12) && (stateCode == '1')){
			tuition = 6000;}


Was This Post Helpful? 0
  • +
  • -

#5 Tiger_Khaze   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 02-April 09

Re: Using multiple "else if" statements?

Posted 25 March 2011 - 07:38 AM

I would use switch control statement instead of multiple if else. I think in this case switch is better to use.
see this for switch syntax: http://www-numi.fnal...rib/switch.html

This post has been edited by Tiger_Khaze: 25 March 2011 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Using multiple "else if" statements?

Posted 25 March 2011 - 07:45 AM

switch(stateCode){

case 1:
//your two if statements related to code # 1
break;

case2:
//two separate if statements related to code # 2
break;

//default case goes here
}


much better practice -- easier on the eyes :bigsmile:

also, on another note:

it's customary to code if and else if's like this:

if(thisHappens){
//do stuff
}else if(thisOtherThing){
//do other stuff
}else{
//default stuff
}



the end bracket typically goes on the line after your operations perform

This post has been edited by Luckless: 25 March 2011 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Using multiple "else if" statements?

Posted 25 March 2011 - 08:12 AM

This would make the logic much easier to read:

if (credits < 12)
{
    if (stateCode == '1')
        tuition = 450 * credits;
    else
        tuition = 800 * credits;
}
else
{
    if (stateCode == '1')
        tuition = 6000;
    else
        tuition = 12000;
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1