Solved

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

Posted on 2004-04-03
1
154 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

732 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