if else statements

im not sure if this is the right way to use if,else statements

Page 1 of 1

3 Replies - 1770 Views - Last Post: 04 March 2008 - 06:02 PM Rate Topic: -----

#1 tatertot560  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-March 08

if else statements

Posted 04 March 2008 - 03:20 PM

the assignment:
write a c++ program that interactively prompts the user to enter, an account number (int), the month end balance (float), the type of account S or s = savings, C or c =checking)(char), if the account is checking and the month end balance is less than $2500.00, the program must also prompt for the number of checks written (int).
it must display the account number, account type and interest rate, month-end balance, interest earned( if non zero), service fee (if non zero) check fee (if non zero), and the ending balance (including interest/fees)

when i compile the program it works but the three variables (?) for the account (== 'C' || account == 'c') read as the output. it must be in the way i'm using the if-else statements. i can only show one if it applies to what the user inputs. can someone please tell me what im doing wrong?
thanks!

the part where i start the 'else' statement is where i think i may be doing something wrong

int main()
{
  float balance;
  float finalbal1;
  float finalbal2;
  float finalbal3;
  float intearn1;
  float intearn2;
  float intearn3;
  float monthendbal;
  float amount1;
  float newbal1;
  char account;
  int acctnum;
  int numchecks;
  float interest1;
  float interest2;
  float charge1;
  float charge2;
  float saving1;
  float checkfee;

  cout << fixed << showpoint << setprecision(2);

  cout << "Please enter account number" << endl;
  cin >> acctnum;
  cout << "Please enter month end balance" << endl;
  cin >> monthendbal;
  cout << "Please enter account type" << endl;
  cin >> account;

  if (account == 'S' || account == 's')
  {
	cout << "Account number?" << acctnum << endl;
	cout << "Month End Balance?" << monthendbal << endl;
	cout << "Account type : (C = checking, S = savings)" << account << endl;
	cout << "Account #:" << acctnum << endl;


	intearn3 = monthendbal * SAVING1;
	newbal1 = monthendbal + intearn3;

	cout << "Savings  3.00% Interest Rate" << endl;
	cout << "Month End Balance" << monthendbal << endl;
	cout << "Interest Earned" << intearn3 << endl;
	cout << "Final Balance" << newbal1 << endl;
  }


else
   if  (account == 'C' || account == 'c')
	  {
	 cout << "Account number?" << acctnum << endl;
	 cout << "Month End Balance?" << monthendbal << endl;
	 cout << "Checking  2.00% Interest Rate" << endl;
	 cout << "Month End Balance" << monthendbal << endl;
	 cout << "Interest Earned" << intearn1 << endl;
	 cout << "Final Balance" << finalbal1 << endl;
	if (monthendbal <=2500)

		cout << "Enter number of checks written" << endl;
		cin >> numchecks;

		checkfee = CHARGE2 * numchecks;
		finalbal3 = monthendbal + CHARGE1 + checkfee;

		cout << "Checking  0.00% Interest Rate" << endl;
		cout << "Month End Balance" << monthendbal << endl;
		cout << "Account type : (C = checking, S = savings)" << account << endl;
		cout << "Account #:" << acctnum << endl;
	if (monthendbal >= 10000 && monthendbal <= 99999)

		intearn2 = monthendbal * INTEREST2;
		finalbal2 = monthendbal + intearn2;

		cout << "Checking  5.00% Interest Rate" << endl;
		cout << "Month End Balance" << monthendbal << endl;
		cout << "Interest Earned" << intearn2 << endl;
		cout << "Final Balance" << finalbal2<< endl;
	if (monthendbal >= 2500 && monthendbal <= 9999)

		intearn1 = monthendbal * INTEREST1;
		finalbal1 = monthendbal + intearn1;
		cout << "Checking  2.00% Interest Rate" << endl;
		cout << "Month End Balance" << monthendbal << endl;
		cout << "Interest Earned" << intearn1 << endl;
		cout << "Final Balance" << finalbal1 << endl;
				  
  return 0;
}


//////////



This post has been edited by tatertot560: 04 March 2008 - 04:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: if else statements

#2 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: if else statements

Posted 04 March 2008 - 03:59 PM

if (balance == 10,00 <=99,999) should be if ( balance >= 10000 && balance <= 99999 ). The same with if (balance == 2500<=9999) - it should be if ( balance > 2500 && balance <= 9999 )

To check two variables, (or to check a single variable in a range), you have to check first one end, and then check the other end. There is no built in functionality to automatically check if a variable's value is within a certain range.

Also, you don't need the commas in the values. It's proper English (or French, or Italian, or whatever), but the compiler doesn't understand it. The language was not designed with support for comma-separated numbers. (Damned if I know what the proper name is for that, but y'all get what I mean. :) )

Beyond that, I don't understand your question. :)

This post has been edited by MorphiusFaydal: 04 March 2008 - 04:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 tatertot560  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-March 08

Re: if else statements

Posted 04 March 2008 - 04:12 PM

View PostMorphiusFaydal, on 4 Mar, 2008 - 03:59 PM, said:

if (balance == 10,00 <=99,999) should be if ( balance >= 10000 && balance <= 99999 ). The same with if (balance == 2500<=9999) - it should be if ( balance > 2500 && balance <= 9999 )

To check two variables, (or to check a single variable in a range), you have to check first one end, and then check the other end. There is no built in functionality to automatically check if a variable's value is within a certain range.

Also, you don't need the commas in the values. It's proper English (or French, or Italian, or whatever), but the compiler doesn't understand it. The language was not designed with support for comma-separated numbers. (Damned if I know what the proper name is for that, but y'all get what I mean. :) )

Beyond that, I don't understand your question. :)


i did change what you suggested to be changed and it helped. but when i compile it i get this as the output


Please enter account number
456
Please enter month end balance
1000
Please enter account type
c
Account number?456
Month End Balance?1000.00
Account type : (C = checking, S = savings)c
Account #:456
Checking 5.00% Interest Rate
Month End Balance1000.00
Interest Earned0.00
Final Balance1000.00
Checking 2.00% Interest Rate
Month End Balance1000.00
Interest Earned-1.78
Final Balance998.22
Enter number of checks written
2
Checking 0.00% Interest Rate
Month End Balance1000.00
Service fee10.00
Check fee0.30
Final Balance1010.30



since i have three different variables in the checking account part, i should only have one output when the user puts in the appropriate input ( like checking balance < 2500, it should work for that one part, and not do any of the others. etc) am i making sense? :)

This post has been edited by tatertot560: 04 March 2008 - 04:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: if else statements

Posted 04 March 2008 - 06:02 PM

Yeah... You're missing the curly braces.

ex:
cin >> someVariable;

if ( someVariable < 10 )
{
    cout << "Execute this code!" << endl;
    cout << "Now I said!" << end;
}
else if ( someVariable > 10 )
{
    cout << "You can dance if you want to" << endl;
    cout << "You can leave your friends behind." << endl;
}
else if ( someVariable == 10 )
{
    cout << "Cause if your friends don't dance and if they don't dance" << endl;
    cout << "And if they don't dance, well, they're no friends of mine." << endl;
}

cout << "Some arbitrary statement to execute afterwards... " << endl;


This post has been edited by MorphiusFaydal: 04 March 2008 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1