Display bonus amount (learning functions)

I'm getting 56 errors on this. I'm still learning the basics of functions and how exactly to set them up...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Calculates and displays a bonus amount


#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
double getSales ();
double calcBonus (double);

int main()
{	
	double sales = getSales();
	double bonus = calcBonus(sales);

	cout << fixed << setprecision(2);
	cout << "The bonus amount is " << bonus << endl;

    return 0;
}   //end of main function

//*****function definitions*****
getSales()
{
	double sales = 0.0;
	cin << sales;
	return sales;
}

calcBonus (double sales)
{
	bonus = sales * .1;
	return bonus;
}
On line 29 and 36 you aren't specifying the return type
On line 32 you should use >> instead of <<
In calcBonus you are assigning to a variable called 'bonus' which wasn't declared in that scope
( you can just return sales * .1 without using any variable )
Line 29 and 36: You didn't specify the return type of the function.
Line 32: << is the extraction operator (used with cout, not cin).
Line 38: bonus is undeclared in the function.
I'm getting an error on line 20: cannot pass function with zero arguments. But when I enter double sales, sales, or just double it gives me back more errors. what am I missing?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Calculates and displays a bonus amount


#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
double getSales (double);
double calcBonus (double);

int main()
{	
	double sales = getSales();
	double bonus = calcBonus(sales);

	cout << fixed << setprecision(2);
	cout << "The bonus amount is " << bonus << endl;

    return 0;
}   //end of main function

//*****function definitions*****
double getSales(double sales)
{
	cin >> sales;
	return sales;
}

double calcBonus (double sales, double bonus)
{
	sales = 0.0;
	bonus = 0.0;
	bonus = sales * .1;
	return bonus;
}
Remove the parameter bonus on line 35, remove line 37 as it will overwrite the argument passed
still wont work. i don't get it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Calculates and displays a bonus amount


#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
double getSales (double);
double calcBonus (double);

int main()
{	
	double sales = getSales(sales);
	double bonus = calcBonus(sales);

	cout << fixed << setprecision(2);
	cout << "The bonus amount is $" << bonus << endl;

    return 0;
}   //end of main function

//*****function definitions*****
double getSales(double sales)
{
	cin >> sales;
	return sales;
}

double calcBonus (double sales)
{
	double bonus = 0.0;
	bonus = sales * .1;
	return bonus;
}
What's wrong with it?
the compiler is giving me a warning when compiling it, then when it executes the program it gives me an error pop up saying debug error: sales is being used without being initialized. I initialized sales into the getSales function and still giving me this error pop up. frustration!!!
double sales = getSales(sales); //here


You may be assigning the return value of the getSales function to the sales variable,
but you are passing a copy of it's uninitialised value to the function to begin with - that is what
the compiler is complaining about.
Last edited on
I didn't see that, anyway the use of parameters in your program isn't correct. You should read the tutorial: http://www.cplusplus.com/doc/tutorial/functions/
i read the functions I tutorial. am i not declaring something right? im getting 1 error and its giving me this looong error message i'm not going to bother with posting..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//Calculates and displays a bonus amount


#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
double getSales ();
double calcBonus (double, double);

int main()
{	
	double sales = getSales();
	double bonus = calcBonus(bonus, sales);

	cout << "Enter sales: $";
	cin >> getSales();
	
	cout << fixed << setprecision(2);
	cout << "The bonus amount is $" << bonus << endl;

    return 0;
}   //end of main function

//*****function definitions*****
double getSales()
{
	double sales = 0.0;
	return sales;
}

double calcBonus (double bonus, double sales)
{
	bonus = sales * .1;
	return bonus;
}
I think your problem is with this line:

 
cin >> getSales();


Im pretty sure you cant cin a function, but Im not to sure on that. At least we have never done it in my class. However, you can change all this:

1
2
3
4
5
using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;


to just:

using namespace std;
@j3tt,
There are some mistakes in your code.
 
double sales = getSales() //sales = 0.0 

1
2
3
double bonus = calcBonus(bonus, sale);
//bonus is undeclared in main(). it's only declared as an argument in calcBonus()
//sales is also 0.0, from previous function 


And as ljrobison said, you can't cin >> that particular function.

@ljrobison,
It's actually better to use using std::what_i_need over the whole namespace. If you include the whole namespace, it's like using all that extra code in your file, which is unnecessary and a waste of space.
Last edited on
Thanks bluezor. I didn't really know that, my teacher just kinda said, you'll use everything in the std library alot so just put using namespace std; at the top of everything. Does it use more memory/cpu or something?

It's actually better to use using std::what_i_need over the whole namespace. If you include the whole namespace, it's like using all that extra code in your file, which is unnecessary and a waste of space.


actually that's untrue it makes no difference either way. BUT YOU SHOULDN'T use namespace std;
because you don't know what is in it yet, so using std::cin etc, you won't run into problems.
at least until you learn everything inside standard namespace and what traps to avoid.
Last edited on
My bad D: I just found a page which explains it pretty well - http://www.cplusplus.com/forum/beginner/15930/

You should mainly read the responses by Helios.
yea realistically it shouldn't be used at all, but code looks extremely ugly and bulky without it.
at least in my eyes, heres an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

class StringSort
{
  public:
        const static int arraySize=100;
        const static std::string usage="Usage -i input -o output";

        StringSort(std::string infl,std::string outfl);
        StringSort();
        void setInFileName(std::string infl);
        void setOutFileName(std::string outfl);
        std::string getOutFileName();
        std::string getInFileName();
        void runApp();
        std::string inputArray(std::string sSArray[],std::string getInFileName(),int limit);
        std::string outputArray(std::string sSArray[],std::string getOutFileName(),bool ended);

  private:
        std::string temp;
        std::string inFile;
        std::string outFile;
        int limit;
        int elementCount;
        std::string junk;
};

and that's only the header file :S doesn't include all the file handling, cout, cin, cerr, exit etc etc etc you use throughout code.
Last edited on
It doesn't look that messy for me xD. For that case though, you can see that you're just using std::string, so an easy using std::string; will do the job :D
yea pretty pisspoor example if you ask me LOL, anyway verging on trolling now.

quick help the OP!!!

cin >> getSales();

you can't do this because getSales(); <---- takes no parameters, so reading something in via keyboard you can't pass it to getSales();, you perhaps could if you had say:

cin >> variable;

getSale(variable); with the getSale method looking like:

double getSale(int number)
{
double sales = 0.0;
return sales;

// but then you probably want to do something with the variable 'number' perhaps assign it to sales?
}


you really need to go scratch your code, as you methods variables don't really make sense.
or fix it so it does, ask yourself what does the code need to do, then write it down in english.
for example, last night I completed an assignment in 2-3hours, that we were given 5 weeks to accomplish(bar a few stackdump errors)... and the reason is because of pseudocode:
task was to read in a file containing any number of lines. store the lines in an array with no duplicate lines, and output to another file.

I have been sitting playing with command line args for about 1 week, when i decided to give up on it and write the rest of my code, which only took me a couple hours. Because i sat down before writing anything and wrote pseudocode with pad and pen. If I hadn't done this my code would probably be alot more bulky, and not systematic. perhaps even came accross multiple errors. And probably taken 10 times longer
the pseudocode was as follows;
::input::
-handle exceptions ......(try / catch)
- create input stream obj ...... (ifstream)
- check if file opened ...... (conditional if statement)
- while not end of file and array is not full ...... ((! input.eof())&&(array[size-1].empty()))
- grab next line from file ...... (getline)
- traverse array and check if line exists ...... (for (array)) if line==array[i] boolean = true;
- else if it doesn't store the line in the array
::done for that method::
- sort array ...... (sort ( array, arraSize); <---- solved this syntax with a post, thanks baz :D
::output::
- handle excpetions ...... (try/catch )
- create output stream obj ...... (ofstream )
- store array in file ...... (for i=0;arraysize;++ )
- don't store null strings ...... (if (! array[i].empty()) output << array[i] << endl; )

done easy as pie.

So try to write an english like method to solve your problem, just as I have done... If you can do this then you are a programmer syntax is irrelevant, , if oyu can write pseudocode, then that's all you need to be a programmer the code simply follows, and syntax can always be looked up, logic cannot.
Last edited on
Topic archived. No new replies allowed.