Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need do an error check when user inputs info.  When I run the program with an error it keeps on flashing the messge.

Posted on 2005-05-03
6
Medium Priority
?
164 Views
Last Modified: 2010-04-01
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
      cout.precision(2);
      cout.setf(ios::fixed);
      //****INITIALIZE****
      int            Shares = 0; //# OF STOCK SHARES OWNED
      double      MarketPrice      = 0;//PRICE PER SHARE OF STOCK
      double      CommissionFee = 0.02;//COMMISSION FEE PER STOCK
      double      CommissionMinimumFee = 32.95; //MINIMUM COMMISSION FEE
      
      
      //****PROGRAM TITLE*****
      cout << "\t\t\t**********STOCK PROGRAM**********\n\n"<<endl;
      
      //****USER INPUT*****                              
      cout << "Please enter amount of shares:  "<<endl;
      cin >> Shares;
      while (Shares < 1){
            cout << "You entered an invalid information, Please try again!" <<endl;
            cin >> Shares;
      }

      cout << "Please enter price per share:   "<<endl;
      cin >> MarketPrice;
      while (MarketPrice < 1){
            cout << "You entered invalid information, Please try again!" <<endl;
            cin >> MarketPrice;
      }

      //****CALCULATION*****
      double      MarketValue      = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
      double      CommissionPaid = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK

      //****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
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13923207
0
 
LVL 86

Expert Comment

by:jkr
ID: 13923214
Try

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

using namespace std;

int ReadInt() {

    int n = 0;
    string tmp;
    char* pCnvEnd;

    do {

        getline(cin,tmp);

        n = (int) strtol(tmp.c_str(),&pCnvEnd,10);

        if (NULL != *pCnvEnd) cout << "You entered an invalid information, Please try again!" <<endl;

    } while(NULL != *pCnvEnd || n < 1);

    return n;
}

int main()
{

    cout.precision(2);
    cout.setf(ios::fixed);
    //****INITIALIZE****
    int          Shares = 0; //# OF STOCK SHARES OWNED
    double     MarketPrice     = 0;//PRICE PER SHARE OF STOCK
    double     CommissionFee = 0.02;//COMMISSION FEE PER STOCK
    double     CommissionMinimumFee = 32.95; //MINIMUM COMMISSION FEE
     
   
     //****PROGRAM TITLE*****
    cout << "\t\t\t**********STOCK PROGRAM**********\n\n"<<endl;
   
     //****USER INPUT*****                        
     cout << "Please enter amount of shares:  "<<endl;

    Shares = ReadInt ();

    cout << "Please enter price per share:   "<<endl;
    MarketPrice = ReadInt ();


    //****CALCULATION*****
    double     MarketValue     = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
    double     CommissionPaid = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK

    //****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*****
}

It's aways better to have a single function take care of such things. BTW, in your code, 'MarketPrice' is always '0', so the output is kinda incorrect.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13923219
Oh, and I used 'strtol()' because the input function this way  also is able to handle alphanumeric input, i.e. won't fail if the user enters some letters.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:mmorse0971
ID: 13923279
If the user enters a dollar amount it returning an error.  If I want them to either enteran dollar amount ex. 65.75 or just enter 60.  Currently if the user enters 60 it returns correctly if they enter 65.75 it returns as an error.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13923334
>>if they enter 65.75 it returns as an error.

Sorry, missed the part about the doubles:

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

using namespace std;

int ReadInt() {

    int n = 0;
    string tmp;
    char* pCnvEnd;

    do {

        getline(cin,tmp);

        n = (int) strtol(tmp.c_str(),&pCnvEnd,10);

        if (NULL != *pCnvEnd) cout << "You entered an invalid information, Please try again!" <<endl;

    } while(NULL != *pCnvEnd || n < 1);

    return n;
}

int ReadDouble() {

    double d = 0;
    string tmp;
    char* pCnvEnd;

    do {

        getline(cin,tmp);

        d = strtod(tmp.c_str(),&pCnvEnd);

        if (NULL != *pCnvEnd) cout << "You entered an invalid information, Please try again!" <<endl;

    } while(NULL != *pCnvEnd);

    return d;
}

int main()
{

    cout.precision(2);
    cout.setf(ios::fixed);
    //****INITIALIZE****
    int          Shares = 0; //# OF STOCK SHARES OWNED
    double     MarketPrice     = 0;//PRICE PER SHARE OF STOCK
    double     CommissionFee = 0.02;//COMMISSION FEE PER STOCK
    double     CommissionMinimumFee = 32.95; //MINIMUM COMMISSION FEE
     
   
     //****PROGRAM TITLE*****
    cout << "\t\t\t**********STOCK PROGRAM**********\n\n"<<endl;
   
     //****USER INPUT*****                        
     cout << "Please enter amount of shares:  "<<endl;

    Shares = ReadInt ();

    cout << "Please enter price per share:   "<<endl;
    MarketPrice = ReadDouble ();


    //****CALCULATION*****
    double     MarketValue     = MarketPrice * Shares;//CALCULATION TO DETERMINE VALUE OF STOCKS
    double     CommissionPaid = CommissionFee * Shares;//CALCULATION TO DETERMINE COMMISSION PER STOCK

    //****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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 2000 total points
ID: 13924275
I don't get the failures you are talking of. I could enter 60 or 65.75 and there is no difference in behavior.

What compiler and platform are you working on?

Did you try to have string input only as jkr has suggested?

Regards, Alex
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
Suggested Courses

810 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