Click here to Skip to main content
16,016,789 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am new to C++. Im trying to get this program to run properly. i cannot, for the life of me, find my mistake. I'm sure it will be obvious to many.

it compiles, but when it runs it just goes straight to a system pause.

#include <iostream>
using namespace std;


void description();
double getDataz();
int printDataz(double salesAmount, double commissionRate, double totalBonus);

int main()
{
     cout << "main entered";
     description();
     cout << "description called";
     double salesAmount, commissionRate, totalBonus = getDataz();
     cout << "getdataz called";
     printDataz(salesAmount, commissionRate, totalBonus);
     cout << "printdataz called";
     return 0;
}

void description()
{
     cout << "This program will calculate the commission rate and total bonus "
          << "for an associate's sales";
     return;
}

double getDataz()
{
       double salesAmount;
       double commissionRate;
       double totalBonus;
       
       
       
       cout << "\n\n\nPlease enter the sales amount:";
       cin  >> salesAmount;
       
       if      (salesAmount < 20000) commissionRate = .04;
       else if  (salesAmount >= 20000 && salesAmount < 30000) commissionRate = .05;
       else if  (salesAmount >= 30000 && salesAmount < 40000) commissionRate = .06;
       else if  (salesAmount >= 40000 && salesAmount < 50000) commissionRate = .07;
       else if  (salesAmount >= 50000) commissionRate = .09;
       
       totalBonus = salesAmount * commissionRate;
       
       return salesAmount, commissionRate, totalBonus;
}
       
int printDataz(double salesAmount, double commissionRate, double totalBonus)
{
     cout << "\n\nThe commission rate for $"<<salesAmount<<" is: " << commissionRate<<endl;
     cout << "The total bonus is: $"<<totalBonus;
     system("pause");
     return 0;
}



[modified] Removed "help me!!" in 2 locations. [\modified]

thanks!!
Posted
Updated 3-Mar-11 7:58am
v2

hhaysMr_Gbg wrote:
return salesAmount, commissionRate, totalBonus;

You cannot return 3 values from a C++ function (only a value is allowed).
Code compiles because the comma operator discards all the values but the last.

A simple solution would be passing the three parameters by reference:
C++
void getDataz(double & salesAmount, double & commissionRate, double & totalBonus)
{
      cout << "\n\n\nPlease enter the sales amount:";
       cin  >> salesAmount;
       
       if      (salesAmount < 20000) commissionRate = .04;
       else if  (salesAmount >= 20000 && salesAmount < 30000) commissionRate = .05;
       else if  (salesAmount >= 30000 && salesAmount < 40000) commissionRate = .06;
       else if  (salesAmount >= 40000 && salesAmount < 50000) commissionRate = .07;
       else if  (salesAmount >= 50000) commissionRate = .09;
       
       totalBonus = salesAmount * commissionRate;
}



[update]
This program
C++
#include <iostream>
using namespace std;


void description();
void getDataz(double & salesAmount, double & commissionRate, double & totalBonus);
int printDataz(double salesAmount, double commissionRate, double totalBonus);

int main()
{
    double salesAmount, commissionRate, totalBonus;
     cout << "main entered";
     description();
     cout << "description called";
      getDataz(salesAmount, commissionRate, totalBonus);
     cout << "getdataz called";
     printDataz(salesAmount, commissionRate, totalBonus);
     cout << "printdataz called";
     return 0;
}

void description()
{
     cout << "This program will calculate the commission rate and total bonus "
          << "for an associate's sales";
     return;
}

void getDataz(double & salesAmount, double & commissionRate, double & totalBonus)
{
  /*
       double salesAmount;
       double commissionRate;
       double totalBonus;
    */


       cout << "\n\n\nPlease enter the sales amount:";
       cin  >> salesAmount;

       if      (salesAmount < 20000) commissionRate = .04;
       else if  (salesAmount >= 20000 && salesAmount < 30000) commissionRate = .05;
       else if  (salesAmount >= 30000 && salesAmount < 40000) commissionRate = .06;
       else if  (salesAmount >= 40000 && salesAmount < 50000) commissionRate = .07;
       else if  (salesAmount >= 50000) commissionRate = .09;

       totalBonus = salesAmount * commissionRate;

    //   return salesAmount, commissionRate, totalBonus;
}

int printDataz(double salesAmount, double commissionRate, double totalBonus)
{
     cout << "\n\nThe commission rate for $"<<salesAmount<<" is: " << commissionRate<<endl;
     cout << "The total bonus is: $"<<totalBonus;
     system("pause");
     return 0;
}

produces the following, on my system (Windows 7, Visual Studio 2010):


main enteredThis program will calculate the commission rate and total bonus for
an associate's salesdescription called


Please enter the sales amount:100
getdataz called

The commission rate for $100 is: 0.04
The total bonus is: $4Press any key to continue . . .


[/update]

:)
 
Share this answer
 
v3
Comments
hhays 3-Mar-11 14:10pm    
thanks!

i edited as per your suggestion, but now i get a compile error:

[Linker error] undefined reference to `getDataz()'
ld returned 1 exit status
F:\School work\CS1315\cpps\Makefile.win [Build Error] [introductory14.exe] Error 1

hmmm.

my new code:

[CODE] #include <iostream>
using namespace std;


void description();
void getDataz();
int printDataz(double salesAmount, double commissionRate, double totalBonus);

int main()
{
double salesAmount;
double commissionRate;
double totalBonus;

cout << "main entered";
description();
cout << "description called";
getDataz();
cout << "getdataz called";
printDataz(salesAmount, commissionRate, totalBonus);
cout << "printdataz called";
return 0;
}

void description()
{
cout << "This program will calculate the commission rate and total bonus "
<< "for an associate's sales";
return;
}

void getDataz(double & salesAmount, double & commissionRate, double & totalBonus)
{
/*double salesAmount;
double commissionRate;
double totalBonus;
*/


cout << "\n\n\nPlease enter the sales amount:";
cin >> salesAmount;

if (salesAmount < 20000) commissionRate = .04;
else if (salesAmount >= 20000 && salesAmount < 30000) commissionRate = .05;
else if (salesAmount >= 30000 && salesAmount < 40000) commissionRate = .06;
else if (salesAmount >= 40000 && salesAmount < 50000) commissionRate = .07;
else if (salesAmount >= 50000) commissionRate = .09;

totalBonus = salesAmount * commissionRate;
return;

}

int printDataz(double salesAmount, double commissionRate, double totalBonus)
{
cout << "\n\nThe commission rate for $"<<salesAmount<<" is: " << commissionRate<<endl;
cout << "The total bonus is: $"<<totalBonus;
system("pause");
return 0;
}[/CODE]
CPallini 3-Mar-11 14:29pm    
The ampersand '&' is missing in the function getDataz prototype.
hhays 3-Mar-11 14:46pm    
the following is still going straight to a system pause:

#include <iostream>
using namespace std;


void description();
void getDataz(double &, double &, double &);
int printDataz(double, double, double);

int main()
{
double salesAmount;
double commissionRate;
double totalBonus;

cout << "main entered";
description();
cout << "description called";
getDataz(salesAmount, commissionRate, totalBonus);
cout << "getdataz called";
printDataz(salesAmount, commissionRate, totalBonus);
cout << "printdataz called";
return 0;
}

void description()
{
cout << "This program will calculate the commission rate and total bonus "
<< "for an associate's sales";
return;
}

void getDataz(double & salesAmount, double & commissionRate, double & totalBonus)
{
/*double salesAmount;
double commissionRate;
double totalBonus;
*/


cout << "\n\n\nPlease enter the sales amount:";
cin >> salesAmount;

if (salesAmount < 20000) commissionRate = .04;
else if (salesAmount >= 20000 && salesAmount < 30000) commissionRate = .05;
else if (salesAmount >= 30000 && salesAmount < 40000) commissionRate = .06;
else if (salesAmount >= 40000 && salesAmount < 50000) commissionRate = .07;
else if (salesAmount >= 50000) commissionRate = .09;

totalBonus = salesAmount * commissionRate;
return;

}

int printDataz(double salesAmount, double commissionRate, double totalBonus)
{
cout << "\n\nThe commission rate for $"<<salesAmount<<" is: " << commissionRate<<endl;
cout << "The total bonus is: $"<<totalBonus;
system("pause");
return 0;
}


wtf
Harrison H 3-Mar-11 14:52pm    
I'm surprised it's even compiling with the line "#include using namespace std;". Even so, is it even printing your first line of output? If it isn't, something else is amiss, because you don't even call on any of your functions before the first call to std::cout <<.
hhays 3-Mar-11 15:00pm    
I'm a new student in C++. The syntax for namespace is correct, its just not posting properly on this webpage. im actually including iostream. but you're right, i'm not getting the first output.
ok, now first thing is to write include files.

[code]
#include <iostream> //this file includes I/O operations
using namespace std; //using directive to allow I/O operations
[/code]

And void function doesn't return anything, so don't use return in your void functions.





regards,
aash
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900