Displaying the menu using a void function

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;
}
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?!

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
ok, but I was told that a void function can be used to display the menu. My question is, how?
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
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();
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?
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 <<
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
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
Topic archived. No new replies allowed.