Calling functions in a menu

Hi, I'm new to the board and I'm having trouble getting my functions to call properly as options in a menu

I need the program to calculate the average rainfall for a season. i created a menu with the seasons, user selects one and the program prompts for three months of rainfall. it then calculates the average and returns to main menu.

the program worked great here is the part i am having problems with

I need to separate everything out to its own function and have it call the function.

i added the welcome screen function then press any key, the menu is displayed and a user selects an option, then the welcome screen is displayed again. i cant figure out why its not calling my 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
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
//Matthew Beard
//Program 3

#include <iostream>
#include <iomanip>
using namespace std;

//Functions
void showWelcome();
void showMenu();
int showSpring();
int showSummer();
int showFall();
int showWinter();



int main()
{
    int choice;     //menu choice
    
    
    //constants for menu choices
    const int SPRING_CHOICE = 1,
              SUMMER_CHOICE = 2,
              FALL_CHOICE = 3,
              WINTER_CHOICE = 4,
              QUIT_CHOICE = 5;
    
    cout << fixed << showpoint << setprecision(1); //sets to 1 decimal place
    
    do
    {
         showWelcome(); // Show Welcome screen
         showMenu(); // Display Menu
         cin >> choice;
         
         //Validate menu selection
         while (choice < SPRING_CHOICE || choice > QUIT_CHOICE)
         {
               cout << "Please enter a valid menu choice: ";
               cin >> choice;
         }
         
         //If user does not want to quit, proceed.
         if (choice != QUIT_CHOICE)
         {
                    switch (choice)
                    {
                           case SPRING_CHOICE:
                                int showSpring();
                                break;
                           
                           case SUMMER_CHOICE:
                                int showSummer();
                                break;
                                
                           case FALL_CHOICE:
                                int showFall();
                                break;
                           
                           case WINTER_CHOICE:
                                int showWinter();
                                break;
                    }
         }
         } while (choice != QUIT_CHOICE);
         return 0;
}
//Welcome Function
void showWelcome()
{
     cout << "Welcome to the average rainfall calculator" << endl << endl;
     system ("pause");
}

//Menu Function

void showMenu()
{    
    cout << "Please choose a season" << endl << endl
         << "1. Spring" << endl
         << "2. Summer" << endl
         << "3. Fall" << endl
         << "4. Winter"<< endl 
         << "5. Quit" << endl << endl;
}
    
//Spring choice function

int showSpring(float spring, float spring1, float spring2, float spring3)
{
    cout << "Enter the rainfall for month one " << endl;
    cin >> spring1;
    cout << endl << "Enter the rainfall for month two " << endl;
    cin >> spring2;
    cout << endl << "Enter the rainfall for month three " << endl;
    cin >> spring3;
    spring = (spring1 + spring2 + spring3) / 3;
    cout << endl << "The average rainfall for Spring was "
         << spring << endl << endl;
    return 0;
}

//Summer choice function

int showSummer(float summer, float summer1, float summer2, float summer3)
{
    cout << endl << "Enter the rainfall for month one " << endl;
    cin >> summer1;
    cout << endl << "Enter the rainfall for month two " << endl;
    cin >> summer2;
    cout << endl << "Enter the rainfall for month three " << endl;
    cin >> summer3;
    summer = (summer1 + summer2 + summer3) / 3;
    cout << endl << "The the average rainfall for Summer was "
         << summer << endl << endl;
    return 0;
}

//Fall choice function

int showFall(float fall, float fall1, float fall2, float fall3)
{
    cout << endl << "Enter the rainfall for month one " << endl;
    cin >> fall1;
    cout << endl << "Enter the rainfall for month two " << endl;
    cin >> fall2;
    cout << endl << "Enter the rainfall for month three " << endl;
    cin >> fall3;
    fall = (fall1 + fall2 + fall3) / 3;
    cout << endl << "The average rainfall for Fall was "
         << fall << endl << endl;
    return 0;
}

//Winter choice function

int showWinter(float winter, float winter1, float winter2, float winter3)
{
    cout << endl << "Enter the rainfall for month one " << endl;
    cin >> winter1;
    cout << endl << "Enter the rainfall for month two " << endl;
    cin >> winter2;
    cout << endl << "Enter the rainfall for month three " << endl;
    cin >> winter3;
    winter = (winter1 + winter2 +winter3) / 3;
    cout << endl << "The average rainfall for Winter was "
         << winter << endl << endl;
    return 0;
}
When you call the functions from within the switch statement remove the 'int' from in front of them. The while loop on line 67 after the brace also appears to be redundant.
If i remove the int from infront of the function i get a linker error: undefined reference to 'showSpring()' any ideas?
I'll have another look. By the way, ignore my previous comment about the 'while' - I missed the 'do' statement further up! :-p
No problem, thanks for the help. i've been trying to figure it out, it seems like a looping problem but i haven't messed with the loop statements. They worked fine before i started making everything its own function.
Last edited on
I removed one of my functions under switch and made it a standard cout << "test";
it works fine. this tells me the problem is my function isn't being called and displayed
Right, first of all, your function prototypes (the ones before main up there) have no parameters, where as your function definitions (the ones below main with all of the stuff in them) have three float parameters each.

Since you're not actually passing anything in to the function right now, you don't need parameters in those definitions at the bottom, so they should be removed.

You could then declare variables inside the functions, for example float summer, summer2 etc.

Generally speaking, you're writing a lot more code then you need to be here. All four of those functions are doing essentially the same thing, you could have one function that works out the averages of the passed in parameters, something like:

1
2
3
4
5
6
7
8
9
10
11
12
// Definition

double GetAverage(double x, double y, double z)
{
   return (x+y+z)/3;
}


// A call would look like this

double avg;
avg = GetAverage(1.5, 2.5, 3.5);


Or something similar to that.
Last edited on
i dug around in my c++ book and found out the prototype parameters just as you posted it.

thanks for the info, i didnt think about writing a function to do the average instead of doing it each and every time.

Cheers! thanks for the help
works fine now, appreciate the help. now i need to add the function for averaging

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
155
156
//Matthew Beard
//Program 3

#include <iostream>
#include <iomanip>
using namespace std;

//Functions
void showWelcome();
void showMenu();
void showSpring(float, float, float, float);
void showSummer(float, float, float, float);
void showFall(float, float, float, float);
void showWinter(float, float, float, float);


int main()
{
    int choice;     //menu choice
    
    float spring, spring1, spring2, spring3,
          summer, summer1, summer2, summer3,
          fall, fall1, fall2, fall3,
          winter, winter1, winter2, winter3;
    
    //constants for menu choices
    const int SPRING_CHOICE = 1,
              SUMMER_CHOICE = 2,
              FALL_CHOICE = 3,
              WINTER_CHOICE = 4,
              QUIT_CHOICE = 5;
    
    cout << fixed << showpoint << setprecision(1); //sets to 1 decimal place
    
    do
    {
         showWelcome(); // Show Welcome screen
         showMenu(); // Display Menu
         cin >> choice;
         
         //Validate menu selection
         while (choice < SPRING_CHOICE || choice > QUIT_CHOICE)
         {
               cout << "Please enter a valid menu choice: ";
               cin >> choice;
         }
         
         //If user does not want to quit, proceed.
         if (choice != QUIT_CHOICE)
         {
                    switch (choice)
                    {
                           case SPRING_CHOICE:
                                showSpring(spring, spring1, spring2, spring3);
                                break;
                                
                           
                           case SUMMER_CHOICE:
                                showSummer(summer, summer1, summer2, summer3);
                                break;
                                
                           case FALL_CHOICE:
                                showFall(fall, fall1, fall2, fall3);
                                break;
                           
                           case WINTER_CHOICE:
                                showWinter(winter, winter1, winter2, winter3);
                                break;
                    }
                    
         }
    } while (choice != QUIT_CHOICE);
      return 0;
}
//Welcome Function
void showWelcome()
{
     cout << "Welcome to the average rainfall calculator" << endl << endl;
     system ("pause");
}

//Menu Function

void showMenu()
{    
    cout << "Please choose a season" << endl << endl
         << "1. Spring" << endl
         << "2. Summer" << endl
         << "3. Fall" << endl
         << "4. Winter"<< endl 
         << "5. Quit" << endl << endl;
}
    
//Spring choice function

void showSpring(float spring, float spring1, float spring2, float spring3)
{
    cout << "Enter the rainfall for month one " << endl;
    cin >> spring1;
    cout << endl << "Enter the rainfall for month two " << endl;
    cin >> spring2;
    cout << endl << "Enter the rainfall for month three " << endl;
    cin >> spring3;
    spring = (spring1 + spring2 + spring3) / 3;
    cout << endl << "The average rainfall for Spring was "
         << spring << endl << endl;
    
}

//Summer choice function

void showSummer(float summer, float summer1, float summer2, float summer3)
{
    cout << endl << "Enter the rainfall for month one " << endl;
    cin >> summer1;
    cout << endl << "Enter the rainfall for month two " << endl;
    cin >> summer2;
    cout << endl << "Enter the rainfall for month three " << endl;
    cin >> summer3;
    summer = (summer1 + summer2 + summer3) / 3;
    cout << endl << "The the average rainfall for Summer was "
         << summer << endl << endl;
    
}

//Fall choice function

void showFall(float fall, float fall1, float fall2, float fall3)
{
    cout << endl << "Enter the rainfall for month one " << endl;
    cin >> fall1;
    cout << endl << "Enter the rainfall for month two " << endl;
    cin >> fall2;
    cout << endl << "Enter the rainfall for month three " << endl;
    cin >> fall3;
    fall = (fall1 + fall2 + fall3) / 3;
    cout << endl << "The average rainfall for Fall was "
         << fall << endl << endl;
    
}

//Winter choice function

void showWinter(float winter, float winter1, float winter2, float winter3)
{
    cout << endl << "Enter the rainfall for month one " << endl;
    cin >> winter1;
    cout << endl << "Enter the rainfall for month two " << endl;
    cin >> winter2;
    cout << endl << "Enter the rainfall for month three " << endl;
    cin >> winter3;
    winter = (winter1 + winter2 + winter3) / 3;
    cout << endl << "The average rainfall for Winter was "
         << winter << endl << endl;
    
}
If this is for some sort of classwork then, in the interest of time, I'd be careful about changing much of your structure. If you're code is functioning the way you want it to and you have the time, maybe you could copy the project and work on other ways of doing it separately. That is to say, don't go changing something you know that works only to be left with something that doesn't work and no copies of your original program.

If you were to refactor it, it's a good idea to write down on paper (or in notepad) the functionality you need. In your case, it would be something like this:

1
2
3
4
5
6
7
8
9
10
11

// Show welcome message

// Enter program loop
// -- Show choice of season
// -- Get input from user
// -- Ask use for float values
// -- Work out average of values
// -- Print output
// Return to start of loop


Personally, I find that doing that makes it easier for me to break up code into separate areas of functionality and getter a better understanding of what I need to write.
thanks for the advice. the next part in the assignment was to separate it out into different functions. otherwise i would just leave it alone.

im having trouble trying to use one function and have it save into different variables each time its called. example, i want a function to ask the user for a number and save it to a variable then the same function call twice more saving into different variables. and yes this is also part of the assignment so i cant just write three different 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

//Matthew Beard
//Program 3

#include <iostream>
#include <iomanip>
using namespace std;

//Functions
void showWelcome();
void showMenu();
float showAvg(float, float, float, float);
float getMonth(float);



int main()
{
    int choice;     //menu choice
    
    float month1, month2, month3,
          average;
    
    //constants for menu choices
    const int SPRING_CHOICE = 1,
              SUMMER_CHOICE = 2,
              FALL_CHOICE = 3,
              WINTER_CHOICE = 4,
              QUIT_CHOICE = 5;
    
    cout << fixed << showpoint << setprecision(1); //sets to 1 decimal place
    
    do
    {
         showWelcome(); // Show Welcome screen
         showMenu(); // Display Menu
         cin >> choice;
         
         //Validate menu selection
         while (choice < SPRING_CHOICE || choice > QUIT_CHOICE)
         {
               cout << "Please enter a valid menu choice: ";
               cin >> choice;
         }
         
         //If user does not want to quit, proceed.
         if (choice != QUIT_CHOICE)
         {
                    switch (choice)
                    {
                           case SPRING_CHOICE:
                                getMonth(month1);
                                getMonth(month2);
                                getMonth(month3);
                                showAvg(average, month1, month2, month3);
                                break;
                                
                           
                           case SUMMER_CHOICE:
                                getMonth(month1);
                                getMonth(month2);
                                getMonth(month3);
                                showAvg(average, month1, month2, month3);
                                break;
                                
                           case FALL_CHOICE:
                                getMonth(month1);
                                getMonth(month2);
                                getMonth(month3);
                                showAvg(average, month1, month2, month3);
                                break;
                           
                           case WINTER_CHOICE:
                                getMonth(month1);
                                getMonth(month2);
                                getMonth(month3);
                                showAvg(average, month1, month2, month3);
                                break;
                    }
                    
         }
    } while (choice != QUIT_CHOICE);
      return 0;
}
//Welcome Function
void showWelcome()
{
     cout << "Welcome to the average rainfall calculator" << endl << endl;
     system ("pause");
}

//Menu Function

void showMenu()
{    
    cout << "Please choose a season" << endl << endl
         << "1. Spring" << endl
         << "2. Summer" << endl
         << "3. Fall" << endl
         << "4. Winter"<< endl 
         << "5. Quit" << endl << endl;
}


//month function

float getMonth(float month1)
{
      cout << "Enter rainfall amount " << endl;
      cin >> month1;
      return month1;
     
}


//Average function

float showAvg(float average, float month1, float month2, float month3)
{
    
    average = (month1 + month2 + month3) / 3.0;
    cout << endl << "The average rainfall was "
         << average << endl << endl;
    
}
First of all, sorry for the late reply. I was writing from work earlier and I've been out all.

Ok, I understand what you're trying to do, and you're very close.

You want the three calls to getMonth to store the month's rainfall into the three different variables you're passing in, yes?

Thing is, take a look at your getMonth function. It returns a value, so whatever value is being returned is present in the function call. The only problem you have, is your calls aren't assigning the variable anywhere. So they're returning it, but not storing it anywhere.

For example:

1
2
3
4
5
6
7
8
9
10
// Currently you have calls like this:
getMonth(month1);

// It's fine, and perfectly valid, but your function has this line:
return month1;

// So where is that return value being stored?  It isn't.  You need to store it somewhere:
month1 = getMonth(month1);

// That's saying "Assign whatever is returned from this function into month1" 


There are ways to assign variables inside functions without returning values, namely passing-by-reference, but you'll probably come to that a little later in.

If there's anything you don't understand there, just let me know and I'll see if I can help out.
Topic archived. No new replies allowed.