[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

when running program I receive an error "runtime check failure #3

Posted on 2005-05-02
1
Medium Priority
?
233 Views
Last Modified: 2011-04-14
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
      cout.precision(2);
      cout.setf(ios::fixed);
      //****INITIALIZE****
      int            Shares;                                          
                double      MarketPrice;                                    
                double      CommissionFee = 0.02;            
      double      CommissionMinimumFee = 32.95;      
      double      MarketValue      = MarketPrice * Shares;                  
                double      CommissionPaid = CommissionFee * Shares;      
      
      //****PROGRAM TITLE*****
      cout << "\t\t\t**********STOCK PROGRAM**********\n\n"<<endl;
      
      //****USER INPUT*****                              
      cout << "Please enter amount of shares:  "<<endl;
      cin >> Shares;

      cout << "Please enter price per share:   "<<endl;
      cin >> MarketPrice;
      
      //****DISPLAY*****
      cout << "Shares:\t\t\t " << Shares << endl;
      cout << "Market Price Per Share:\t $" << MarketPrice << endl;
      cout << "Commission Fee:\t\t $" << CommissionFee << endl;
      cout << "Commission Minimum Fee:\t $" <<CommissionMinimumFee << endl;

      cout<< "\n\nMarket Value:\t\t $" << MarketValue << endl;
      
      //****CONDITIONAL STATEMENT*****
      if (CommissionPaid < CommissionMinimumFee) {
      
            cout << "Commission Paid:\t$" <<CommissionMinimumFee << endl;
      }
      else if (CommissionPaid > CommissionMinimumFee) {
            
            cout <<"Commission Paid:\t$" <<CommissionPaid<< endl;
            
      }

cin.ignore();
cin.ignore();
//*****END PROGRAM*****
}
0
Comment
Question by:mmorse0971
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13914426
Try to initialize all your variables before using them, e.g.

     //****INITIALIZE****
    int          Shares = 0;                                  
                double     MarketPrice = 0.0;      

And, do the calculations *after* the user has entered the values:

int main()
{
    cout.precision(2);
    cout.setf(ios::fixed);
    //****INITIALIZE****
    int          Shares;                                  
                double     MarketPrice;                              
               double     CommissionFee = 0.02;          

    //****PROGRAM TITLE*****
    cout << "\t\t\t**********STOCK PROGRAM**********\n\n"<<endl;
   
     //****USER INPUT*****                        
     cout << "Please enter amount of shares:  "<<endl;
    cin >> Shares;

    cout << "Please enter price per share:   "<<endl;
    cin >> MarketPrice;
   
    double     CommissionMinimumFee = 32.95;    
     double     MarketValue     = MarketPrice * Shares;              
                double     CommissionPaid = CommissionFee * Shares;    
     
     //****DISPLAY*****
    cout << "Shares:\t\t\t " << Shares << endl;
    cout << "Market Price Per Share:\t $" << MarketPrice << endl;
    cout << "Commission Fee:\t\t $" << CommissionFee << endl;
     cout << "Commission Minimum Fee:\t $" <<CommissionMinimumFee << endl;

    cout<< "\n\nMarket Value:\t\t $" << MarketValue << endl;
   
     //****CONDITIONAL STATEMENT*****
    if (CommissionPaid < CommissionMinimumFee) {
   
          cout << "Commission Paid:\t$" <<CommissionMinimumFee << endl;
    }
    else if (CommissionPaid > CommissionMinimumFee) {
         
         cout <<"Commission Paid:\t$" <<CommissionPaid<< endl;
         
    }

cin.ignore();
cin.ignore();
//*****END PROGRAM*****
}

which produces:

                        **********STOCK PROGRAM**********


Please enter amount of shares:
5
Please enter price per share:
1.20
Shares:                  5
Market Price Per Share:  $1.20
Commission Fee:          $0.02
Commission Minimum Fee:  $32.95


Market Value:            $6.00
Commission Paid:        $32.95

Apart from that, the above runs fine here.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question