Now that my program is growing in size and I have quiet a few display functions is it possible to put all these display functions in another header file eg. display_functions.h

My objective is to clean up my main() file so I dont have to scroll up and down all day.

I tryed to do it earlier and the project did compile.

HOWEVER when it called my displayfunction I entered which option I wanted i.e. 1 but it never returned the no back to the main();

eg.

Choose which option:

cout<<1. blah blah
cout<<2 blah blah

cin>>no
return no; // value of no was not returned to main()

My Question in short: IS IT POSSIBLE TO RETURN the value of no TO THE main() program.

thx in advance for advice

Dani AI

Generated

Yes — returning a value from a function placed in a header is possible, but two things caused the behaviour seen in the thread: the return value wasn't saved in main, and defining non-inline functions directly in a header is fragile. correctly shows that main must capture the returned value; is also correct that main should return int.

Keep only declarations in the header and put definitions in a .cpp file. Example header skeleton (use include guards or #pragma once):

#ifndef DISPLAY_H
#define DISPLAY_H

int showMenu();   // prototype only

#endif /* DISPLAY_H */

Implementation goes in a single .cpp file. Use std:: names or using in the .cpp only, and validate input before returning:

#include "display.h"
#include <iostream>

int showMenu() {
    int choice = 0;
    std::cout << "1) Add patient\n2) Remove patient\n> ";
    while (!(std::cin >> choice) || choice < 1 || choice > 2) {
        std::cin.clear();
        std::cin.ignore(10000, '\n');
        std::cout << "Enter 1 or 2: ";
    }
    return choice;
}

Finally, capture the return in main and return from main:

#include "display.h"

int main() {
    int selection = showMenu();
    switch (selection) {
        case 1: /* add */ break;
        case 2: /* remove */ break;
    }
    return 0;
}

Notes and cautions: do not put full non-inline function definitions in a header that will be included by multiple .cpp files (linker errors). Avoid using namespace std; in headers. An alternative pattern is to have the menu fill an output parameter (void getMenu(int &out)), but returning the choice is clearer for simple menus.

Recommended Answers

All 4 Replies

Tim,

As I mentioned in my email, you've got to make this question a lot clearer....

sorry about being vague but perhaps this will throw more light on the situation.

int display_function(); //declaration of function outside main


main()
{

display_function(); //call function
}


int display_function() //fuction to display
{
cout<<"choose option"<<endl;
cout<<1. Add patient"<<endl;
cout<<2. Remove patient"<<endl;

cin>>option;
return option;
}

Currently I have 4 menus to display. and by the time I have completed the prog I will have a few more. What I want to do is move all my display functions to a seperate header file eg. display.h (at the moment I am scrolling up and down all the time, I want to make the code easier to find)

#include "display.h"
int display_function(); //declaration of function outside main

main()
{
display_function(); //call function

                 switch (option){

                         case 1:
                         cout<<"Patients details can be added here"<<endl;
                         break;

                        case 2:
                       cout<<"Patient can be removed from here"<<endl;
                       break;
}

}


//then in the display.h file I have all my display functions
int display_function() //fuction to display
{
cout<<"choose option"<<endl;
cout<<1. Add patient"<<endl;
cout<<2. Remove patient"<<endl;
cin>>option;
return option;
}

when I tryed this earlier the project did compile and run.

up in the screen it displayed:

choose option
1. Add patient
2. Remove patient

when I entered the number (1) i expected the value to be returned to the main program so I could use it in my switch statment however this was not the case.

My Question in short: IS IT POSSIBLE TO RETURN the value of OPTION TO THE main() program

Yes it is possible. But the problem is you are not saving it to a variable in the main function. The variable

option

is declared inside the display_function so the main function does not know about it. Change your code as below and it will work.

#include "display.h"
int display_function(); //declaration of function outside main

main()
{
int userOption = display_function(); //call function

switch (userOption ){

case 1:
cout<<"Patients details can be added here"<<endl;
break;

case 2:
cout<<"Patient can be removed from here"<<endl;
break;
}

}

You can even use the name option instead of userOption, but I used this name to emphasis that it is different from the one declared in display_function().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.