Experiencing errors when calling functions.

Hi, my compiler keeps giving me the following errors when i call my functions in main()

"use of undeclared identifier"(error1)

"Expected"(" for function-style cast or type construction.(error2)

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

//declaring.
void Displaymenu();
void Add(int,int);
void Subtract(int,int);
void Multiply(int ,int);
void Divide(double, double);

//rest of  code in main() and other function calls are here.

int main()
{
    int choice;
    string continu = "y";
    string username;
    
    //menu choice constants
    const int ADD_CHOICE = 1;
    const int SUB_CHOICE = 2;
    const int MULT_CHOICE = 3;
    const int DIV_CHOICE = 4;
    const int QUIT_CHOICE = 5;
  
    
    //setting up output.
    cout << fixed << showpoint << setprecision(2);
    
  //prompt user to continue.
   cout << "Press y to continue" << endl;
    cin >> continu;
    
    // prompt for user name
    cout << "Please enter your name " << endl;
    cin >> username;

    
    //test for validation.

    
    do
    {
        //display the menu  ask for input.
        Displaymenu();
        cin >> choice;
       
        
        //Validate user choice.
        while (choice < ADD_CHOICE || choice > QUIT_CHOICE)
        {
            cout << "Please enter a valid menu choice: " << endl;
            cin >> choice;
        }
       
        //continue with program if user does not choose to quit.
        if (choice !=QUIT_CHOICE)
        {
            
            //perform the necessary execution and operations depending on user choice.
           
            switch (choice)
            {
                case ADD_CHOICE:
                    Add(add1, add2); // error1 undeclared identifiers add1 and add2
                
                break;
                
                case SUB_CHOICE:
                  Subtract(sub1, sub2); // error1, sub1 and sub2 undeclared identifiers
                  
                
                break;
                
                case MULT_CHOICE:
                    Multiply(mut1, mut2); // error1, mut1 and mute same as above
                   
                
                break;
                
                case DIV_CHOICE:
                    Divide(double div1, double div2); // error 2
                   
                
                break;
             
            }
        }
    } while (choice!=QUIT_CHOICE);
    cout << "good bye" << endl;
    

    return 0;
}

// defining said functions outside of main(), 

void Add(int add1, int add2)
{
    srand((unsigned)time(0));
    add1 = rand() % 100 +1;
    add2 = rand() % 100 +1;
    cout << "***** ADDITION *******" << endl;
    cout << "********************** " << endl;
    cout << "***" << add1 << "+" << add2 <<"=" << "?" << "***" << endl;
    
}

void Subtract(int sub1, int sub2)
{
    srand((unsigned)time(0));
    sub1 = rand() % 100 +1;
    sub2 = rand() % 100 +1;
    cout << "****** SUBTRACTION ***** " << endl;
    cout << "************************ " << endl;
    cout << "***" << sub1 << "-" << sub2 << "=" << "?" << "***" << endl;
    
}

void Multiply(int mut1, int mut2)
    {
        srand((unsigned)time(0));
        mut1 = rand() % 100 +1;
        mut2 = rand() % 100 +1;
        cout << "****** MULTIPLICATION *****" << endl;
        cout << "***************************" << endl;
        cout << "***" << mut1 << "*" << mut2 << "=" << "?" << "***" << endl;
        
    }
void Divide(double div1, double div2) //Expected "(" error here. 
{
    srand((unsigned)time(0));
    div1 = rand() % 100 +1;
    div2 = rand() % 100 +1;
    cout << "***** DIVISION *******" << endl;
    cout << "**********************" << endl;
    cout << "***" << div1 << "/" << div2 << "=" << "?" << "***" << endl;
}

void Displaymenu()
{
  
    cout << " 1. ADD    " << endl;
    cout << " 2. SUBTRACT"  << endl;
    cout << " 3. MULTIPLY "  << endl;
    cout << "4. DIVIDE     "  << endl;
    cout << " 5. QUIT       "  << endl;
    cout << " Please select an option between 1-4, select 5 to quit" << endl;
    
    
}

this is a program that will display a menu, and ask the user to select a choice(1-4), afterwards it will generate a math problem based on the selection and the user will enter the answer. This is still a work in progress.
Last edited on
Post main.

Also, for now on, make sure your put your code in code brackets so it will be more readable.
Displaymenu();

you didnt declare this function.

Add(add1, add2);

the variables add1 and add2 are not declared.
Display menu declared.


Oh, i'm supposed to declare those variables(add,sub,mut and div) in the main() correct?


I think that will solve the issue, i'm going to correct it and compile.


Heres the corrected code, replaced add1,add2, sub1&2 etc with variable1 and variable2. It works :D!, now its time to finish the rest and implement validation. Thanks guys!

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

//declaring.
void Displaymenu();
void Add(int,int);
void Subtract(int,int);
void Multiply(int ,int);
void Divide(double, double);

//rest of  code in main() and other function calls are here.

int main()
{
    int choice;
    string continu = "y";
    string username;
    int variable1;
    int variable2;

    //menu choice constants
    const int ADD_CHOICE = 1;
    const int SUB_CHOICE = 2;
    const int MULT_CHOICE = 3;
    const int DIV_CHOICE = 4;
    const int QUIT_CHOICE = 5;
  
    
    //setting up output.
    cout << fixed << showpoint << setprecision(2);
    
  //prompt user to continue.
   cout << "Press y to continue" << endl;
    cin >> continu;
    
    // prompt for user name
    cout << "Please enter your name " << endl;
    cin >> username;

    
    //test for validation.

    
    do
    {
        //display the menu  ask for input.
        Displaymenu();
        cin >> choice;
       
        
        //Validate user choice.
        while (choice < ADD_CHOICE || choice > QUIT_CHOICE)
        {
            cout << "Please enter a valid menu choice: " << endl;
            cin >> choice;
        }
       
        //continue with program if user does not choose to quit.
        if (choice !=QUIT_CHOICE)
        {
            
            //perform the necessary execution and operations depending on user choice.
           
            switch (choice)
            {
                case ADD_CHOICE:
                    Add(variable1,variable2);
                
                break;
                
                case SUB_CHOICE:
                  Subtract(variable1, variable2); 
                  
                
                break;
                
                case MULT_CHOICE:
                    Multiply(variable1, variable2); 
                   
                
                break;
                
                case DIV_CHOICE:
                    Divide(variable1, variable2); 
                   
                break;
             
            }
        }
    } while (choice!=QUIT_CHOICE);
    cout << "good bye" << endl;
    

    return 0;
}

// defining said functions outside of main(), 

void Add(int variable1, int variable2)
{
    srand((unsigned)time(0));
    variable1 = rand() % 100 +1;
    variable2 = rand() % 100 +1;
    cout << "***** ADDITION *******" << endl;
    cout << "********************** " << endl;
    cout << "***" << variable1 << "+" << variable2 <<"=" << "?" << "***" << endl;
    
}

void Subtract(int variable1, int variable2)
{
    srand((unsigned)time(0));
    variable1 = rand() % 100 +1;
    variable2 = rand() % 100 +1;
    cout << "****** SUBTRACTION ***** " << endl;
    cout << "************************ " << endl;
    cout << "***" << variable1 << "-" << variable2 << "=" << "?" << "***" << endl;
    
}

void Multiply(int variable1, int variable2)
    {
        srand((unsigned)time(0));
        variable1 = rand() % 100 +1;
        variable2 = rand() % 100 +1;
        cout << "****** MULTIPLICATION *****" << endl;
        cout << "***************************" << endl;
        cout << "***" << variable1 << "*" << variable2 << "=" << "?" << "***" << endl;
        
    }
void Divide(double variable1, double variable2) 
{
    srand((unsigned)time(0));
    variable1 = rand() % 100 +1;
    variable2 = rand() % 100 +1;
    cout << "***** DIVISION *******" << endl;
    cout << "**********************" << endl;
    cout << "***" << variable1 << "/" << variable2 << "=" << "?" << "***" << endl;
}

void Displaymenu()
{
  
    cout << " 1. ADD    " << endl;
    cout << " 2. SUBTRACT"  << endl;
    cout << " 3. MULTIPLY "  << endl;
    cout << "4. DIVIDE     "  << endl;
    cout << " 5. QUIT       "  << endl;
    cout << " Please select an option between 1-4, select 5 to quit" << endl;
    
    
}
Last edited on
Ok, now I have finaly figured out what do you want to do. You are doing it the wrong way. And you are not understanding the point of functions.

You can try reading this
http://www.programming4beginners.com/tutorial/functions/functions

Unfortunately, the remaining (and greater) part of that tutorial which further explains functions is only available in the printed edition:

http://www.amazon.com/Programming-Beginners-C-Kevin-Compton/dp/1511806982/
Thank you for the link.
Topic archived. No new replies allowed.