• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Program exits after user input

#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>

using namespace std;

int main()
{
       char            Answer;
     
//*****Loop*****
do{
       //****PROGRAM TITLE*****
     cout << "\t\t\t*************"<<endl;
       cout << "\t\t\tSTOCK PROGRAM      "<<endl;
       cout << "\t\t\t*************\n\n"<<endl;
      
     //****DEFINE VARIABLES****
     int        Shares = 0; //# OF STOCK SHARES OWNED
     double     MarketPrice = 0.00;//PRICE PER SHARE OF STOCK
     double     CommissionFee = 0.02;//COMMISSION FEE PER STOCK
     double     CommissionMinimumFee = 39.95; //MINIMUM COMMISSION FEE
       double     MarketValue; //TOTAL VALUE
       double     CommissionPaid; //COST OF COMMISSION
       double            TotalAmountPaidCF;
       double            TotalAmountPaidCMF;
       int            UserMenu;
       double            NYSE = 0.00;
       double            AMEX = 0.00;
       double            OTC = 0.00;
       double            NASDAQ = 0.00;       
      
       //****SET 2 DECIMAL PLACES*****
       cout << setprecision(2);
       cout.setf(ios::fixed);
       cout.setf(ios::right);

       //****USER MENU*****
       cout << "\nNYSE\t = 1" << endl;
       cout << "\nAMEX\t = 2" << endl;
       cout << "\nOTC\t = 3" << endl;
       cout << "\nNASDAQ\t = 4" << endl;
      
       cout << "\nTo QUIT\t = 5" <<endl;
            
       cout << "\nPlease Choose Stock Market:  ";
             
       //****USER INPUT FOR STOCK MARKET******
       cin >> UserMenu;

       //****SWITCH*****
       switch (UserMenu){
             case 1:
                   CommissionFee = 0.02;
             case 2:
                   CommissionFee = 0.025;
             case 3:
                   CommissionFee = 0.03;
             case 4:
                   CommissionFee = 0.05;
             case 5:
                   return 0;

       }
             
     //****USER INPUT FOR SHARES*****
       while (Shares <= 0){
             cout << endl;
             cout<< "Please enter number of shares: ";
             cin >> Shares;
     
       //****ERROR CHECK*****
             if (Shares <= 0)
                   cout << endl << "You entered invalid information, Please try again!" <<endl;
       }

       //****USER INPUT FOR MARKET PRICE*****
       while (MarketPrice <= 0.00){
             cout << endl;
             cout<< "Please enter Market Price Per Share: $";
             cin >> MarketPrice;
     
       //****ERROR CHECK*****
             if (MarketPrice <= 0)
                   cout << endl << "You entered invalid information, Please try again!" <<endl;
       }
      
     //****CALCULATION*****
     MarketValue = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
     CommissionPaid      = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK
       TotalAmountPaidCMF = CommissionMinimumFee + MarketValue;
       TotalAmountPaidCF = MarketValue + CommissionPaid;
      
       //****DISPLAY*****
     cout << "\n\nShares:\t\t\t\t " << Shares << endl;
     cout << "\nMarket Price Per Share:\t\t $" << MarketPrice << endl;
     cout << "\nMarket Value:\t\t\t $" << MarketValue << endl;
      
       //****CONDITIONAL STATEMENT FOR COMMISSION*****
     if (CommissionPaid < CommissionMinimumFee) {
            cout << "\nCommission Paid is :\t\t $" << CommissionMinimumFee << endl;
            cout << "\n\n\nTotal Amount Paid:\t\t  $" << TotalAmountPaidCMF << endl;
     }

     else if (CommissionPaid > CommissionMinimumFee) {
            cout << "\nCommission Paid is :\t\t $" << CommissionPaid << endl;    
            cout << "\n\n\nTotal Amount Paid:\t\t  $" << TotalAmountPaidCF << endl;
     }

       //****DISPLAY USER OPTIONS TO END OR CONTINUE PROGRAM*****
       cout << "\n\nDo you want to continue enter (Y/N) then press enter?\n";
       cout<< "\nYou must type a 'Y' or an 'N'.";
     cin >> Answer;

//****END LOOP*****
}
      //****CONDITIONAL STATMENT FOR LOOP*****
      while ((Answer == 'Y') || (Answer == 'y'));
            if (Answer = 'N')
                  return 0;
            else if (Answer = 'n')
                  return 0;         
cin.ignore();
cin.ignore();

//*****END PROGRAM*****
}
0
mmorse0971
Asked:
mmorse0971
  • 3
1 Solution
 
LuxanaCommented:
in your case switch use break !!!

//****SWITCH*****
      switch (UserMenu){
           case 1:
                CommissionFee = 0.02;
break;
           case 2:
                CommissionFee = 0.025;
break;
           case 3:
                CommissionFee = 0.03;
break;
           case 4:
                CommissionFee = 0.05;
break;
           case 5:
                return 0;

      }
0
 
LuxanaCommented:
If you do not use "break" switch reads all cases and finish with return 0 which is terminate program..


hope this help

./lubo
0
 
mmorse0971Author Commented:
I knew I forgot something but figured it out thanks for your help.  However, next question could I take what I have and use an array instead?  If so how could I achieve this?

#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>

using namespace std;

int main()
{
       char            Answer;
     
//*****Loop*****
do{
       //****PROGRAM TITLE*****
     cout << "\t\t\t*************"<<endl;
       cout << "\t\t\tSTOCK PROGRAM      "<<endl;
       cout << "\t\t\t*************\n\n"<<endl;
      
     //****DEFINE VARIABLES****
     int        Shares = 0; //# OF STOCK SHARES OWNED
     double     MarketPrice = 0.00;//PRICE PER SHARE OF STOCK
     double     CommissionFee = 0.02;//COMMISSION FEE PER STOCK
     double     CommissionMinimumFee = 39.95; //MINIMUM COMMISSION FEE
       double     MarketValue; //TOTAL VALUE
       double     CommissionPaid; //COST OF COMMISSION
       double            TotalAmountPaidCF;
       double            TotalAmountPaidCMF;
       int            UserMenu;
       string            StockMarket;
       double            NYSE = 0.00;
       double            AMEX = 0.00;
       double            OTC = 0.00;
       double            NASDAQ = 0.00;       
      
       //****SET 2 DECIMAL PLACES*****
       cout << setprecision(2);
       cout.setf(ios::fixed);
       cout.setf(ios::right);

       //****USER MENU*****
       cout << "\nNYSE\t = 0" << endl;
       cout << "\nAMEX\t = 1" << endl;
       cout << "\nOTC\t = 2" << endl;
       cout << "\nNASDAQ\t = 3" << endl;
      
       cout << "\nTo QUIT\t = 4" <<endl;
            
       cout << "\nPlease Choose Stock Market:  ";
             
       //****USER INPUT FOR STOCK MARKET******
       cin >> UserMenu;

             
     //****USER INPUT FOR SHARES*****
       while (Shares <= 0){
             cout << endl;
             cout<< "Please enter number of shares: ";
             cin >> Shares;
     
       //****ERROR CHECK*****
             if (Shares <= 0)
                   cout << endl << "You entered invalid information, Please try again!" <<endl;
       }

       //****USER INPUT FOR MARKET PRICE*****
       while (MarketPrice <= 0.00){
             cout << endl;
             cout<< "Please enter Market Price Per Share: $";
             cin >> MarketPrice;
     
       //****ERROR CHECK*****
             if (MarketPrice <= 0)
                   cout << endl << "You entered invalid information, Please try again!" <<endl;
       }
      
      
       //****SWITCH*****
       switch (UserMenu){
             case 0:
                   StockMarket = "NYSE";
                   CommissionFee = 0.02;
                   break;
             case 1:
                   StockMarket = "AMEX";
                   CommissionFee = 0.025;
                   break;
             case 2:
                   StockMarket = "OTC";
                   CommissionFee = 0.03;
                   break;
             case 3:
                   StockMarket = "NASDAQ";
                   CommissionFee = 0.05;
                   break;
             case 4:
                   return 0;

       }
     //****CALCULATION*****
     MarketValue = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
     CommissionPaid      = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK
       TotalAmountPaidCMF = CommissionMinimumFee + MarketValue;
       TotalAmountPaidCF = MarketValue + CommissionPaid;
      
       //****DISPLAY*****
       cout << "\n\nStock Market:\t\t\t" << StockMarket << endl;
     cout << "\n\nShares:\t\t\t\t " << Shares << endl;
     cout << "\nMarket Price Per Share:\t\t $" << MarketPrice << endl;
     cout << "\nMarket Value:\t\t\t $" << MarketValue << endl;
      
       //****CONDITIONAL STATEMENT FOR COMMISSION*****
     if (CommissionPaid < CommissionMinimumFee) {
            cout << "\nCommission Paid is :\t\t $" << CommissionMinimumFee << endl;
            cout << "\n\n\nTotal Amount Paid:\t\t  $" << TotalAmountPaidCMF << endl;
     }

     else if (CommissionPaid > CommissionMinimumFee) {
            cout << "\nCommission Paid is :\t\t $" << CommissionPaid << endl;    
            cout << "\n\n\nTotal Amount Paid:\t\t  $" << TotalAmountPaidCF << endl;
     }

       //****DISPLAY USER OPTIONS TO END OR CONTINUE PROGRAM*****
       cout << "\n\nDo you want to continue enter (Y/N) then press enter?\n";
       cout<< "\nYou must type a 'Y' or an 'N'.";
     cin >> Answer;

//****END LOOP*****
}
      //****CONDITIONAL STATMENT FOR LOOP*****
      while ((Answer == 'Y') || (Answer == 'y'));
            if (Answer = 'N')
                  return 0;
            else if (Answer = 'n')
                  return 0;         
cin.ignore();
cin.ignore();

//*****END PROGRAM*****
}
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now