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

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.