Solved

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

Posted on 2004-04-03
1
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

690 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