Functions and Variables and Scope issue

I'm writing a program that's using several outside functions to simplify the code overall. I have variables declared in int main(), but because of that, the functions I wrote do not have access to them. My teacher has stressed the importance of not using global variables, so I am hesitant to do so. However, it might be the best way to fix the problem. What do you think? Should I do that, or is there another way?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
integration.cpp: In function âvoid rect(double&, double&)â:
integration.cpp:65: error: âsumrectâ was not declared in this scope
integration.cpp:66: error: âarea1â was not declared in this scope
integration.cpp: In function âvoid trap(double&, double&, double&)â:
integration.cpp:70: error: âsumtrapâ was not declared in this scope
integration.cpp:71: error: âarea2â was not declared in this scope
integration.cpp: In function âvoid func1(double&)â:
integration.cpp:75: error: âfaâ was not declared in this scope
integration.cpp: In function âvoid func2(double&)â:
integration.cpp:79: error: âfaâ was not declared in this scope
integration.cpp: In function âvoid func3(double&)â:
integration.cpp:83: error: âfaâ was not declared in this scope
integration.cpp: In function âvoid func4(double&)â:
integration.cpp:87: error: âfaâ was not declared in this scope
integration.cpp: In function âvoid func5(double&)â:
integration.cpp:91: error: âfaâ was not declared in this scope


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
  1 #include<iostream>
  2 #include<cmath>
  3 using namespace std;
  4
  5 void rect (double &width, double &length);
  6 void trap (double &width, double &base1, double &base2);
  7 void func1 (double &input);
  8 void func2 (double &input);
  9 void func3 (double &input);
 10 void func4 (double &input);
 11 void func5 (double &input);
 12
 13 int main (){
 14
 15    int choice1, choice2, numshapes, start, end;
 16    double sumrect, sumtrap, area1, width, input, fa, base1, base2, area2, length, input2;
 17    width = (end-start)/numshapes;
 18    area1 = 0;
 19    area2 = 0;
 20
...
 61    return 0;
 62 }
 63
 64 void rect (double &width, double &length){
 65    sumrect = width*length;
 66    area1 = area1+sumrect;
 67 }
 68
 69 void trap (double &width, double &base1, double &base2){
 70    sumtrap = (base1+base2)*width/2;
 71    area2 = area2+sumtrap;
 72 }
 73
 74 void func1 (double &input){
 75    fa = (5*pow(input,4))+(3*(pow(input,3)))-(10*input)+(2);
 76 }
 77
 78 void func2 (double &input){
 79    fa = (pow(input,2))-(10);
 80 }
 81
 82 void func3 (double &input){
 83    fa = (40*input)+(5);
 84 }
 85
 86 void func4 (double &input){
 87    fa = pow(input,3);
 88 }
 89
 90 void func5 (double &input){
 91    fa = (20*pow(input,2))+(10*input)-(2);
 92 }
Nevermind. Since my functions are one/two lines anyway, I might as well just plug them into main. You can lock this.
Last edited on
closed account (Dy7SLyTq)
you probably shouldn't use global variables. what was wrong with your code, is that your treating & as a "global operator" if you pass say an int called myInt to say myFunction(int &anotherInt), myFunction doesnt have access to myInt it has access to the address of it and can modify it but it has to be done through anotherInt
Topic archived. No new replies allowed.