Functions

Hey I made a new function to my program so it validates the input

But I only linked it from the main to it. But other functions are linked to it to. Please help.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int retry();
int main();
int Invalid();
char cancel();
int addition();

	int addition()
	{
		system("CLS");
		float plus;
		float total = 0;


		cout << "What is the first number you will like to add? or type -1 to cancel. \n";
		cin >> plus;
		total = total + plus;
		system("CLS");
		
		while(plus != -1){
		cout << "what is the next number you will like to add? or type -1 to cancel. \n";
		cin >> plus;
		total = total + plus;
		system("CLS");
		}
		total = total + 1;
		cout << "The numbers you have entered add up to " << total << endl << endl << endl;

		return retry();
}
	int substraction()
	{
		float subtract;
		float total = 0;


		cout << "What is the first number you will like to subtract? or type -1 to cancel. \n";
		cin >> subtract;
		total = total - subtract;
		
		while(subtract != -1){
		cout << "what is the next number you will like to subtract? or type -1 to cancel. \n";
		cin >> subtract;
		total = total + subtract;
		}
		cout << "The numbers you have entered subract to " << total << endl << endl << endl;

		return retry();
	}
	int multiplication()
	{
		float multiplication;
		float total = 0;


		cout << "What is the first number you will like to multiply? or type -1 to cancel. \n";
		cin >> multiplication;
		total = total + multiplication;
		
		while(multiplication != -1){
		cout << "what is the next number you will like to multiply? or type -1 to cancel. \n";
		cin >> multiplication;
		total = total + multiplication;
		}
		cout << "The numbers you have entered equal " << total << endl << endl << endl;

		return retry();
	}
	int division()
	{
		float division;
		float total = 0;


		cout << "What is the first number you will like to Divide? or type -1 to cancel. \n";
		cin >> division;
		total = total + division;
		
		while(division != -1){
		cout << "what is the next number you will like to Divide? or type -1 to cancel. \n";
		cin >> division;
		total = total + division;
		}
		cout << "The numbers you have entered equal " << total << endl << endl << endl;

		return retry();
	}
int Invalid(){
	string input;

	system("CLS");

	cout << "Invalid Input, Try again? y/n \n";
	cin >> input;
		if (input == "y" || input == "Y"){
			system("CLS");
			return main();
		}
	return 0;
}
int retry()
{
	string restart;

	cout << "Would you like to do another sum? y/n \n";
	cin >> restart;

	if (restart == "y" || restart == "Y"){
		system ("cls");
		main();
	}

	return 0;
}
int main()
{
	string myString = "";

	
	cout << "What type of sum would you like to do? + (Addition) - (Substraction) \n x (Multiplication) / (Division) " << endl; 
	cin >> myString;

	if (myString == "+"){
		cout << endl << endl;
		addition();
	}
	else if (myString == "-"){
		cout << endl << endl;
		substraction();
	}
	else if (myString == "x"){
		cout << endl << endl;
		multiplication();
	}
	else if (myString == "/"){
		cout << endl << endl;
		division();
	}
	if (myString != "+" || myString != "-" || myString != "x" || myString != "/" || myString != "n" || myString != "N"){
		Invalid();
	}

	system("PAUSE");
	return 0;
}



But I only linked it from the main to it. But other functions are linked to it to
I don't understand what you are trying to say. ¿Do you mind to rephrase it?

By the way, you can't call main(). It is not allowed by the standard.
Functions are written in the global scope. That means that they can be accessed by any other function (including themselves) in this cpp file as long as they are declared or defined before the other functions.

1
2
3
4
5
6
7
8
9
void func2(); // Declaration of func2

void func1(){
  cout << 1;
}

void func2(){ // Definition of func2
  cout << 2;
}

Function 1 can access function 2 because function 2 is declared above function 1. Function 2 can access function 1 because function 1 is defined above function 2.

1
2
3
4
5
6
7
void func1() {
  cout << 1;
}

void func2() {
  cout << 2;
}

In this case, function 2 can access function 1, but function 1 cannot access function 2 because there is no forward declaration.

To expand the scope of a function so that it is available to other cpp files, you would put the declaration in a header.

To limit the scope of a function, you could put the definition and declaration in a namespace or class (depending on how you are writing things). In the case of a namespace, others would still be able to access that function, but they'd have to manually specify your namespace first. In the case of a class, only other functions in that class would be able to access a private function.
Last edited on
Topic archived. No new replies allowed.