Solved

modifying a program so it uses a program defined function to get and validate sales.

Posted on 2004-04-03
1
150 Views
Last Modified: 2010-04-01
I need to modify this program so that it uses a value-returning function to get and validate the sales amount entered by the user. The function should return a value such as Y or N that indicates if the sales amount is valid.  The return value should determine whether to calculate and display the commission.  My problem is when I enter a negative number as the sales it returns a negative commission instead of the "No commission calculated" message.

//T9AppE10.cpp - displays the commission

#include <iostream>
using namespace std;

//function prototype
float calcCommission(int);
int getAndValidateSales(int);

int main()
{
      int sales        = 0;
      float commission = 0.0;
      int salesData = 0;
      
      //enter input data
      cout << "Enter sales: ";
      cin >> sales;


      //validate input data
      if (salesData = 1)
      {
            //calculate and display commission
            commission = calcCommission(sales);
            cout << "Commission: " << commission << endl;
      }
      else
            cout << "No commission calculated" << endl;
      //end if

      return 0;
}   //end of main function

//*****program-defined functions*****
float calcCommission(int amountSold)
{
      //calculates and returns the commission
      float commDollars = 0.0;

      if (amountSold <= 100000)
            commDollars = amountSold * .02;
      else if (amountSold <= 400000)
            commDollars = 2000 + .05 * (amountSold - 100000);
      else commDollars = 17000 + .1 * (amountSold - 400000);
      //end ifs
      return commDollars;
}      //end of calcCommission function

int getAndValidateSales(int &sales)
{
      int salesData = 0;

      if (sales > 0)
            salesData = 1;
      else salesData = 2;
      return salesData;
}      //end getAndValidateSales
0
Comment
Question by:SinSual
1 Comment
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 500 total points
ID: 10748705
You have not called getAndValidateSales () fuction
call that function after accepting the value of sales and take the return value in salesData

something like

salesData = getAndValidateSales ( sales ) ;
if ( salesData == 1 ) .............

Also, in your if condition, u are assigning 1 to salesData instead of checking it
In C/C++, for checking equality, u should use == as shown above

Amit
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

785 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