several functions

closed account (4wpL6Up4)
Hi,
unfortunately I feel a bit lost.
I am supposed to write a program by first of all declaring all the functions needed and then adding functions definitions.
The program is supposed to take an input from the user, and calculate the discount which is proportional to the input.
I have made many mistakes and I would need a hint in order to aim towards the right direction.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include "pch.h"
#include <stdlib.h>
#include<iostream>
#include<cstdlib>
#include<cctype>
#include <sstream>
#include <string>
#include<cmath>
#include <algorithm>
#include <iterator>
#include <ctype.h>
#include <iomanip>
#include <limits>
#include<cstdlib>


using namespace std;

void load(double *sum);
void discount(double *sum);

void print(double sum, double discountKr);
int main()
{
	double sum = 0.0, discountKr = 0.0;
	char answer;
	do {
		system("CLS"); 
		cout << "Discount" << endl;
		cout << "======" << endl << endl;
		
		load(&sum);
		discount(&sum);
	
		
		

		discount(&sum);
		{
			if (0 <= sum & sum <= 500)
			{
				discount == 0;
			}
			else if (501 <= sum & sum <= 1000)
			{
				discount == 5.0;
			}
			else if (1001 <= sum & sum <= 5000)
			{
				discount == 10;
			}
			else if (5001 <= sum)
			{
				discount == 15;
			}
		}

		discountKr = sum * discount(&sum) / 100;

		sum = sum - discountKr;
		print(sum, discountKr);
		cout << endl << "One more time (Y/N)? ";
		cin >> answer;
	} while (toupper(answer == 'Y');
	return 0;
}

void load(double *sum)
{
	cout << "enter sum" << endl;
	cin >> *sum;
}

void print(double sum, double discountKr)
{
	sum = sum - discountKr;
	print(sum, discountKr);
}
Maybe something like this.
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
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<cctype>

using namespace std;

void load(double &sum);
void discount(double sum, double &discountKr);
void print(double sum, double discountKr);

int main()
{
	double sum = 0.0, discountKr = 0.0;
	char answer;
	do {
		load(sum);
		discount(sum,discountKr);
		print(sum,discountKr);
		cin >> answer;
	} while (toupper(answer) == 'Y');
	return 0;
}

void load(double &sum)
{
	cout << "enter sum" << endl;
	cin >> sum;
}

void discount(double sum, double &discountKr) {
    int percentage;
    if (0 <= sum & sum <= 500)
    {
        percentage = 0;
    }
    else if (501 <= sum & sum <= 1000)
    {
        percentage = 5.0;
    }
    else if (1001 <= sum & sum <= 5000)
    {
        percentage = 10;
    }
    else if (5001 <= sum)
    {
        percentage = 15;
    }
    discountKr = sum * percentage / 100;
}

void print(double sum, double discountKr)
{
    cout << sum << " " << discountKr << " " << (sum - discountKr) << endl;
}
Last edited on
Hello nypran,

Your header files need a little work. You start with "stdlib.h", but with VS this is included through "iostream" Also you include "cstdlib". First this is the same as "stdlib.h" and second you include it twice. You only need it once. "cctype" is the same as "ctype.h". "cctype" like "cstdlib" will wrap the ".h" in the standard name space, so between the two the header files like "cctype" are the better ones to use for a C++ program.

At the moment including "sstream" and "string" are not used in your program. Of the two I might keep "string" in case something changes.

The header files "algorithm" and "iterator" are not used at all in your program. So unless you have something to add to your program you do not need these.

"iomanip" and "limits" are two useful header files. Although you may not need them in this program, I do not know until I see the output, I include them in just about every program I write. With "iomanip" you will find this line of code very useful: std::cout << std::fixed << std::showpoint << std::setprecision(2); The "fixed" means not to use scientific notation, the "showpoint" means print ".00" if it shows up and the "setprecision(2)" means print two decimal places. The "2" can be changed to however many decimal places you need. "iomanip" also has "std::setw()" which can help line up you output to the screen or a file.

Line 17 is best to avoid and not use if you can. There are many posts about why not to use this line. Do a search here to see what has been said.

Your prototypes are OK for the most part, but I would pass by reference instead of using the pointer. Replace "*" with "&".

For line 28.. It is best not to use "system" for anything. First it is specific to Windows and not every can use it. A Unix type operating system uses some similar, but it is completely different. I do have a function that I use that would be safer than using "system" if you are interested.

Line 38 is a function call to a function that you have not defined yet.

Lines 39 and 56 the {}s around the if statements are not needed.

The rest look OK for now. I will know more when I have a chance to run the program.

Your use of "toupper" is good.

Some tips:

The use of {}s. For "int main()" this works best., but for your do/while loop it would be better if it look more line what you did with "int main()". It is best to be consistent with the use of {}s. I also fine that when the {}s line up in the same column and with proper indentation it is easier to find problems when a curley brace is missing.

Foe an "if/else if/else", for loop and while loop that only have a single line the {}s are not necessary, but OK if you use them.

Uour use of blank lines is good, but in some places you could use some and others you could use less. The point is to break up the code to make it easier to read and follow.

Without running the program this is what I see for now.

Hope that helps,

Andy
I usually put {} on one line statements as well, as a style thing, with a reason. The reason is that I will end up debugging anything meaningful and probably will want to stick a print in some of the loops/ifs/etc making them no longer one line, and its easier if they just always have brackets. Often I leave the prints in (commented or defined out) if they have any future diagnostic use. Just some thoughts as you develop your style.
Topic archived. No new replies allowed.