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: TimC is an unknown quantity at this point 
Solved Threads: 0
TimC TimC is offline Offline
Light Poster

returning a value from a header file

 
0
  #1
Dec 4th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: returning a value from a header file

 
0
  #2
Dec 4th, 2005
Tim,

As I mentioned in my email, you've got to make this question a lot clearer....
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 42
Reputation: TimC is an unknown quantity at this point 
Solved Threads: 0
TimC TimC is offline Offline
Light Poster

Re: returning a value from a header file

 
0
  #3
Dec 4th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: returning a value from a header file

 
0
  #4
Dec 4th, 2005
Yes it is possible. But the problem is you are not saving it to a variable in the main function. The variable
  1. 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().
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: returning a value from a header file

 
0
  #5
Dec 4th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC