Solved

# Function calls

Posted on 2005-05-08
181 Views
Last Modified: 2010-04-01
Ok, don't laugh, I'm a newbie :-(.  I have written this program for school and I don't know where or what to do next, or if I need to throw this away and start over.  Can anyone help me?

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setw;
using std::setprecision;

double wages(  ); // function wages prototype
int display(  ); // function display prototype

int main()
{

int salesPerson;
double salesPerWk;
int salary;
int count;

int range[ 11 ] = { 300, 400, 500, 600, 700, 800, 900, 1000 };

for ( int i = 0; i < 11; i++ ) {
count = range[i] + 1;

// call wage function
cout << "Enter salesperson sales per week:  ";
cin >> salary;

// call display function
cout << "Total sales in \$200 - \$299 range are:   ";

} //end main

// define wages function
double wages(salary);

while (salesPerson <= 9 ) {
salary = salesPerWk * .09 + 200;
salesPerson = salesPerson + 1;
}

// define display function
int display;
{
if ( salary < range[300] )
cout << "a";
else if ( salary < range[400] )
cout << "b";
else if ( salary < range[500] )
cout << "c";
else if ( salary < range[600] )
cout << "d";
else if ( salary < range[700] )
cout << "e";
else if ( salary < range[800] )
cout << "f";
else if ( salary < range[900] )
cout << "g";
else if ( salary < range[1000])
cout << "h";

return 0;
} // end if
0
Question by:reldadenise
9 Comments

LVL 86

Accepted Solution

OK, you had a few syntactical and logical errors, the following compiles:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setw;
using std::setprecision;

double wages(  ); // function wages prototype
int display(  ); // function display prototype

int range[ 11 ] = { 300, 400, 500, 600, 700, 800, 900, 1000 };

int salesPerson;
double salesPerWk;
int salary;
int count;

int main()
{

for ( int i = 0; i < 11; i++ ) {
count = range[i] + 1;

// call wage function
cout << "Enter salesperson sales per week:  ";
cin >> salary;

// call display function
cout << "Total sales in \$200 - \$299 range are:   ";

}

return 0;
} //end main

// define wages function
double wages()
{

while (salesPerson <= 9 ) {
salary = salesPerWk * .09 + 200;
salesPerson = salesPerson + 1;
}

return salary;
}

// define display function
int display ()
{
if ( salary < range[0] )
cout << "a";
else if ( salary < range[1] )
cout << "b";
else if ( salary < range[2] )
cout << "c";
else if ( salary < range[3] )
cout << "d";
else if ( salary < range[4] )
cout << "e";
else if ( salary < range[5] )
cout << "f";
else if ( salary < range[6] )
cout << "g";
else if ( salary < range[7])
cout << "h";

return 0;
} // end if

1st of all, there were problems with the synatx regarding e.g.

double wages (); // semicolon incorrect when code follows

Then, the variables you declared in 'main()' need to be "global" to be accessible by the two other functions.
0

Author Comment

Ok, I'll try this.  But, does it look like I actually have any clue as to how C++ works?

Denise
0

LVL 86

Expert Comment

Then, this is the best time to start with that :o)
0

Author Comment

Ok, don't answer that!  One more question:  how do I get the array values to fall into the display function?

Denise
0

Author Comment

Never mind, I figured that out - yea!!!  Thanks for you help!!!

Denise
0

LVL 86

Expert Comment

Th eproblem with the array mainly is that by using an index 'n' into them, you're accessing the 'n'th element, so using '300' with an array of 11 elments will not work. I'd suggest to use

#define SALARY_300 0
#define SALARY_400 1
#define SALARY_500 2
#define SALARY_600 3
#define SALARY_700 4
#define SALARY_800 5
#define SALARY_900 6
#define SALARY_1000 7

// define display function
int display ()
{
if ( salary < range[SALARY_300] )
cout << "a";
else if ( salary < range[SALARY_400] )
cout << "b";
else if ( salary < range[SALARY_500] )
cout << "c";
else if ( salary < range[SALARY_600] )
cout << "d";
else if ( salary < range[SALARY_700] )
cout << "e";
else if ( salary < range[SALARY_800] )
cout << "f";
else if ( salary < range[SALARY_900] )
cout << "g";
else if ( salary < range[SALARY_1000])
cout << "h";

return 0;
}

to map the entries to their ranges by name.

BTW, an advanced step would be using a loop, e.g.

int display ()
{
char alpha[] = "abcdefgh";
int i;
for ( i = SALARY_300; i <= SALARY_1000; ++i)
{
if ( salary < range[i]) cout << alpha[i];
}
return 0;
}
0

LVL 86

Expert Comment

Oops, seems that I have again been a tad bit slow when typing :o)

Hope that gives little more insight.
0

Author Comment

Thanks jkr !!!

Denise
0

Author Comment

A great deal more . . . thanks!
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

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â€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
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 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.

#### 737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!