| | |
returning a value from a header file
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2005
Posts: 42
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: May 2005
Posts: 42
Reputation:
Solved Threads: 0
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
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 is declared inside the display_function so the main function does not know about it. Change your code as below and it will work.
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().
C++ Syntax (Toggle Plain Text)
option
#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;
}
} By the way, main itself should return an int. (having main not return anything is considered bad form)
http://www.delorie.com/djgpp/v2faq/faq22_25.html
http://www.delorie.com/djgpp/v2faq/faq22_25.html
![]() |
Similar Threads
- graphices header file (C)
- Header File (C++)
- header file/ classes (C++)
- compile header file (C++)
- Link source code and header file together? (C++)
- How to write a header file (C++)
Other Threads in the C++ Forum
- Previous Thread: problem with cin>> cin.getline
- Next Thread: Character Arrays
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






