Displaying the menu using a void function

Oct 21, 2012 at 8:31pm
I am trying to familiarize myself with the use of void functions. I am not sure what I am doing wrong here. It is giving me an error.
Any idea?

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
#include<iostream>
#include<cmath>
#include<cstdlib>

using namespace std;
void Display_Menu(void) ;// Prototype

int main(){
    
    

    cout<< Display_Menu()<<endl;
    
    system("PAUSE");
    return 0;
    
}





void Display_Menu(void) {
  cout << endl << "     MAIN MENU" << endl;
  cout << "0 - quit" << endl;
  cout << "1 - Read a file" << endl;
  cout << "2 - Print a record" << endl;
}
Oct 21, 2012 at 8:45pm
void means that there is no any return object. But you are trying to outout something that does not exist.


cout<< Display_Menu()<<endl;

What value are you going to send to cout?!

Oct 21, 2012 at 8:46pm
cout<< Display_Menu()<<endl;
"cout <<" expects to find something on the right of the arrows. A void function doesn't return anything, so there is nothing on the right for cout to display
Last edited on Oct 22, 2012 at 1:03am
Oct 21, 2012 at 9:35pm
ok, but I was told that a void function can be used to display the menu. My question is, how?
Oct 21, 2012 at 9:38pm
It already displays the menu as you wrote it.

1
2
3
4
5
6
void Display_Menu(void) {
  cout << endl << "     MAIN MENU" << endl;
  cout << "0 - quit" << endl;
  cout << "1 - Read a file" << endl;
  cout << "2 - Print a record" << endl;
}

The only thing you need is to call it in main the following way


Display_Menu();
Last edited on Oct 21, 2012 at 9:39pm
Oct 21, 2012 at 11:17pm
you dont need the cout statement in line 12. your void menu is already 'couting' the options, so just run the code like this ;
Display_Menu();
Oct 22, 2012 at 12:57am
Thanks! It's working now. So I guess, with a void function, I do not need the "cout<<", but with any other, I need the "cout<<", correct?
Oct 22, 2012 at 1:00am
Depends on what the function returns. cout can output atomic data types, streams and std::strings. For any other return type you need to overload operator <<
Oct 22, 2012 at 1:04am
No. The function defines what will be output, therefore, you only need to call the function to output what was defined. No matter what type of function it is.

1
2
3
4
5
6
void Display_Menu(void) {
  cout << endl << "     MAIN MENU" << endl;
  cout << "0 - quit" << endl;
  cout << "1 - Read a file" << endl;
  cout << "2 - Print a record" << endl;
}


That function can be called via:

Display_Menu();

Calling that will execute the function and in turn execute what's in it.
Last edited on Oct 22, 2012 at 1:06am
Oct 22, 2012 at 1:15am
But when it output the Menu, it will not let the user choose what option right?
Because there's no input statement for the user to input their options.
I can see that you forgot to put an input statement so that the user can enter their chose.
Last edited on Oct 27, 2012 at 7:43pm
Topic archived. No new replies allowed.