I just started out - made a VAT calc - can you help me?

Mar 6, 2008 at 6:27pm
Hi there folks!. I have just started to learn C++ (and LOVING every second, I must say!). I have written a very basic VAT calculator; would someone tell me if I have made any obvious mistakes please:

Also - could you explain in LAYMANS terms, what the diff is between:

int main() and int main(void)
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
// This application works out how much VAT you pay, allowing custom VAT rates to be set


#include <iostream>
using namespace std;
int main()
{
	
	// declarations here;
	float vatrate = 0.0;
	float exvat = 0.0;
	float total = 0.0;
	float totalvat = 0.0;
	
	// Clears screen of any junk
	system("clear"); // screen clear for Mac OS X; comment out as applicable, between platforms
	// system ("cls"); // screen clear for Win32; comment out as applicable, between platforms
	
	// asks user to set VAT rate
	cout << "Please enter VAT rate, in %: ";
	
	// stores VAT rate that user typed
	cin >> vatrate;
	
	// asks user to enter the amount - excluding VAT - for which they wish to find the applicable VAT
	cout << "Please enter the amount EXCLUDING VAT: ";
	
	// stores ex VAT amount that user typed
	cin >> exvat;
	
	// calculates the total, which includes VAT @ rate set by user
	total = exvat / 100 * vatrate + exvat;
	
	// calculates the total VAT amount
	totalvat = exvat / 100 * vatrate;
	
	// tells user how much they will pay in total, INCLUDING VAT at specified rate
	cout << "The total INCLUDING VAT, is: £" << total << "\n\n\n";
	
	// tells user how much VAT they will pay at specified rate
	cout << "You are paying £" << totalvat << " VAT, at " << vatrate <<"%\n\n\n";
	
	// <blahblah> general copyleft/right information </blahblah>
	cout << "Copyleft, Matt Foot - 6th March 2008\n\n\n";
	
	// system ("pause"); // pauses Win32 console, so app doesn't just exit after completion; comment out for Mac OS X
	
	// App ends
	return 0;
}


Finally, having copy/pasted my source DIRECTLY to this webpage from "textmate" (Mac), I see weird characters that have appeared next to my "£" symbol, that are NOT present in my source code. Would you mind telling me why?. Have I set the encoding wrong, in "textmate" editor app?.

Thanks!.

Last edited on Mar 6, 2008 at 6:39pm
Mar 6, 2008 at 7:11pm
int main(void) and int main() are effectively the same.

For your win32 you can use

1
2
// #define WIN32
// uncomment above for Win32 


1
2
3
4
5
#ifdef WIN32
    system("cls");
#else
    system("clear");
#endif 


1
2
3
#ifdef WIN32
system ("pause");
#endif 


The #define means you only have to make 1 adjustment for Win32 regardless of how many Win32 statements you want to have.

Hotaru
Mar 6, 2008 at 8:42pm
Thanks, Hotaru!. Could you tell me where I put your:

#ifdef.... etc

etc?.

Do I put this here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

// #define WIN32
// uncomment above for Win32

#ifdef WIN32
system ("cls");
#else
system ("clear");
#endif

#ifdef WIN32
system ("pause");
#endif

int main ()

........ etc ..........



[EDIT]

Never mind - I Googled it; I now realise that these are pre-processor statements. I put the #define <insert definition here> BEFORE int main() and then I insert the #ifdef macros, as and when applicable within the function code, of any particular function.

here is the new one:

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
// This application works out how much VAT you pay, allowing custom VAT rates to be set


#include <iostream>
// #define WIN32 
// un-comment the above line, if compiling for WIN32 platform
using namespace std;
int main()
{
	
	// declarations here;
	float vatrate = 0.0;
	float exvat = 0.0;
	float total = 0.0;
	float totalvat = 0.0;
	
	// Clears screen of any junk - these pre-processor commands look for the #define WIN32 command, and act accordingly
	#ifdef WIN32
	system ("cls");
	#else
	system ("clear");
	#endif
	
	// asks user to set VAT rate
	cout << "Please enter VAT rate, in %: ";
	
	// stores VAT rate that user typed
	cin >> vatrate;
	
	// asks user to enter the amount - excluding VAT - for which they wish to find the applicable VAT
	cout << "Please enter the amount EXCLUDING VAT: ";
	
	// stores ex VAT amount that user typed
	cin >> exvat;
	
	// calculates the total, which includes VAT @ rate set by user
	total = exvat / 100 * vatrate + exvat;
	
	// calculates the total VAT amount
	totalvat = exvat / 100 * vatrate;
	
	// tells user how much they will pay in total, INCLUDING VAT at specified rate
	cout << "The total INCLUDING VAT, is: £" << total << "\n\n\n";
	
	// tells user how much VAT they will pay at specified rate
	cout << "You are paying £" << totalvat << " VAT, at " << vatrate <<"%\n\n\n";
	
	// <blahblah> general copyleft/right information </blahblah>
	cout << "Copyleft, Matt Foot - 6th March 2008\n\n\n";
	
	// Pauses execution on WIN32. This pre-processor command looks for the #define WIN32 command, and acts accordingly
	#ifdef WIN32
	system ("pause");
	#endif
	
	// App ends
	return 0;
}
Last edited on Mar 7, 2008 at 12:07am
Topic archived. No new replies allowed.