Solved

If and Else if statments

Posted on 2005-04-26
203 Views
Could someone help with the if and else if statements?

#include <iostream>
#include <iomanip>

using namespace std;

void main()
{

int            Shares                              = 100;
double      MarketPrice                        = 63.25;
double      CommissionFee                  = 0.02;
double      CommissionMinimumFee      = 32.95;

double      MarketValue                        = MarketPrice * Shares;
double      CommissionPaid                  = CommissionFee * Shares;;

//Calculations

//Diplay
cout.precision(2);
cout.setf(ios::fixed);

cout << "Shares:\t\t\t " << Shares << endl;
cout << "Market Price Per Share:\t \$" << MarketPrice << endl;
cout << "CommissionFee:\t\t \$" << CommissionFee << endl;
cout << "Commission Minimum Fee:\t \$" <<CommissionMinimumFee << endl;

cout<< "\n\nMarket Value:\t\t \$" << MarketValue << endl;
cout.setf(ios::scientific);

if (CommissionPaid < CommissionMinimumFee){

cout << "Commission Paid:  \$" << CommissionMinimumFee << endl;
}
else if (Commission Paid > CommissionMinimumFee){

cout <<"Commission Paid: \$" <<CommissionFee<< endl;
}

cin.ignore();
cin.ignore();
}
0
Question by:mmorse0971

LVL 3

Expert Comment

I don't see problems with your code. What exactly do you need help on?

Looking through the code, the if statement will always execute, as Shares*commision will be .02 * 100 = 2. 2 is less than ComissionMinimum of 32.95 and thus if statement will always evaluate to true. Therefore, the comission minimum will be displayed.

Other than that, I see nothing wrong with the code. For the most part...

Mactep
0

Author Comment

error C2065: 'Commission' : undeclared identifier
error C2146: syntax error : missing ')' before identifier 'Paid'
error C2065: 'Paid' : undeclared identifier
error C2059: syntax error : ')'
error C2143: syntax error : missing ';' before '{'

I thought it was fine too but I am getting these errors and I have been trying to figure it out for a few hours.  Thought I would as for some help on this one.
0

LVL 3

Accepted Solution

You have a space between Comission and Paid.

>>   else if (Commission Paid > CommissionMinimumFee){
>>         cout <<"Commission Paid: \$" <<CommissionFee<< endl;
>>     }

The compiler is now treating thhis as two identifiers and neither of them is declared. What you want is one word CommissionPaid, as it is declared as double.

Disregard the
error C2146: syntax error : missing ')' before identifier 'Paid'
and
error C2143: syntax error : missing ';' before '{'

Visual C++ gets confused sometimes.

Hope this helps.

Mactep
0

Author Comment

It must be getting late I knew I should of looked at it again tomorrow.  Thank you.
0

Author Comment

WHen the program runs I do not want the 33 it's taking 32.95 and rounding it off and not adding the decimal point.  How would I include that in my if statements?

#include <iostream>
#include <iomanip>

using namespace std;

void main()
{

int            Shares                              = 100;
double      MarketPrice                        = 63.25;
double      CommissionFee                  = 0.02;
double      CommissionMinimumFee      = 32.95;

double      MarketValue                        = MarketPrice * Shares;
double      CommissionPaid                  = CommissionFee * Shares;

//Diplay
cout.precision(2);
cout.setf(ios::fixed);

cout << "Shares:\t\t\t " << Shares << endl;
cout << "Market Price Per Share:\t \$" << MarketPrice << endl;
cout << "CommissionFee:\t\t \$" << CommissionFee << endl;
cout << "Commission Minimum Fee:\t \$" <<CommissionMinimumFee << endl;

cout<< "\n\nMarket Value:\t\t \$" << MarketValue << endl;
cout.setf(ios::scientific);

if (CommissionPaid < CommissionMinimumFee) {

cout << "Commission Paid:  \$" << CommissionMinimumFee << endl;
}
else if (CommissionPaid > CommissionMinimumFee) {
cout <<"Commission Paid:      \$" <<CommissionPaid<< endl;

}

cin.ignore();
cin.ignore();
}

DIPSLAYS:

Shares:                  100
Market Price Per Share:  \$63.25
CommissionFee:           \$0.02
Commission Minimum Fee:  \$32.95

Market Value:            \$6325.00
Commission Paid:  \$33

0

LVL 3

Expert Comment

No Problem. Have a good night.
Mactep
0

LVL 3

Expert Comment

>> //Diplay
>>     cout.precision(2);

The precision of two means, you only get to see the 32 or 33. Since you want 32.95, set the precision to 4:
cout.precision(4);

Mactep
0

Featured Post

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!