How to Do using functions?

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
#include<cstdlib>
#include<iomanip>

using namespace std;

int main(){
	int courseNo,marks;

	cout<<"Please enter your Course Number: ";
	cin>>courseNo;
	
	if(courseNo==1)
	    {
		cout<<"You have selected Graphics & Multimedia course\n";
		}
		
	else if(courseNo==2)
		{
			cout<<"You have selected Hardware and Networking course\n";
		}
	else
		{
			cout<<"Please select either 1 or 2\n";
			exit(0);
		}
		
	cout<<"Please enter your marks: ";
	cin>>marks;
	
	switch(courseNo){
		case 1:{
			if(marks<45)
	{
		cout<<"You are not selected\n";
		
	}
			else if(marks>=45 && marks<=64)
	{
		cout<<"Your course Fee is : 255 $\n";
	}
			else if(marks>=65 && marks<=79)
	{
		
		cout<<"Your course Fee is :  "<< fixed << setprecision(2) <<(255-(255*.05))<<" $\n";
		
		
	}
	else if(marks>=80 && marks<=100)
	{
		
		cout<<"Your course Fee is :  "<< fixed << setprecision(2) <<(255-(255*.075 ))<<" $\n";
		
		
	}
	else
	{
		cout<<"Please enter a valid mark!\n";
	}
	
	
	
		}
break;
		case 2:{
			if(marks<45)
	{
		cout<<"You are not selected\n";
		
	}
			else if(marks>=45 && marks<=64)
	{
		cout<<"Your course Fee is : 340 $\n";
	}
			else if(marks>=65 && marks<=79)
	{
		
		cout<<"Your course Fee is :  "<< fixed << setprecision(2) <<(340-(340*.05))<<" $\n";
		
		
	}
	else if(marks>=80 && marks<=100)
	{
		
		cout<<"Your course Fee is :  "<< fixed << setprecision(2) <<(340-(340*.075 ))<<" $\n";
		
		
	}
	else
	{
		cout<<"Please enter a valid mark!\n";
	}
	
	
	
		}
		
	}
	
	system("pause");
	return 0;
}




can anyone help me to do this question using functions.Thing I want here is how to prototype and declare functions here.
Last edited on
You can try and do it yourself - http://www.cplusplus.com/doc/tutorial/functions/
To refactor('function out') code:
-First select the portion of code to convert into function
-Mark out the required variables and use them as function parameters
-Replace the code with a function call after implementing your function

In C++, the compiler must see the function before it can be called hence the need for a prototype when the implementation is not 'available.'

1
2
//syntax for prototype:
return_value function_name(parameters);

Check: http://www.cplusplus.com/doc/tutorial/functions/

And pls use code tags for your code presentation; http://www.cplusplus.com/forum/articles/42672/
Topic archived. No new replies allowed.