typedef struct book
{
int bookindex_num ;
int bookprice;
}mybook;
i was create this header file.
my.cpp
#include <iostream.h>
#include <stdio.h>
main()
{
mybook m_book;
m_book.bookindex_num =15;
m_book.bookprice = 150;
return 0;
}
after i was create CPP code for header file. now i want to use this values in main.cpp(other CPP file).is it possible? can u help me?
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
First off you do not need the include files in the header file here. It will not cause any problems if you leave it. Second there is no file "iostream.h" it is just "iostream". And "stdio.h" is not a standard C++ header file. It is not needed for this program. That is in both files.
"my.cpp" is a good start. You give values to the struct variables, but never use them.
As long as you include the "my.h" header fiile in any other ".cpp" file you can use the struct. Such s in main change "stdio.h" to "my.h" and the short program you have will work.
my.cpp
1 2 3 4 5 6 7 8 9 10 11
#include <iostream> // <--- Do not need the .h.
#include <my.h> // <--- Include your header file.
int main() // <--- Neens the return value of "int".
{
mybook m_book;
m_book.bookindex_num =15;
m_book.bookprice = 150;
return 0;
}
#include <iostream>
#include "My.h" // <--- Include in any file that needs this header.
// prototypes
void Print(conststruct book m_book);
int main()
{
mybook m_book; // <--- Defines object of "mybook" here.
m_book.bookindex_num = 15;
m_book.bookprice = 150;
Print(m_book); // <--- Need to pass "m_book" to the function.
// <--- Comment these two lines or delete if not needed.
std::cout << "\n\n\n\n Press Enter to continue";
std::cin.get(); // <--- To keep the window open if needed.
return 0;
}
A third file
1 2 3 4 5 6 7 8 9 10 11 12
// This could easily be in seperate file.
#include <iostream>
#include <iomanip> // <--- Used for fixed, showpoint and setprecision.
#include "My.h" // <--- Include in any file that needs this header.
void Print(conststruct book m_book)
{
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << "\n Book Number: " << m_book.bookindex_num << " at $" << m_book.bookprice << std::endl;
}
I need to apologize for my haste in my previous message. I forgot to include thee function call to "Print" in min and the parameter the the function needs. I will correct that.
The concept that I presented will work for whatever you need with the necessary changes. Use it as an idea of what to do not what it has to be as it is written.
in third file i ll call bookprice it is go to header and read value from my.cpp and return value exist in third .cpp
The header file only defines the struct. It is the definition in main that creates the object and allows you to store information. This needs to be passed to other functions in order to use the information stored in the struct.
I called the function "Print", but you can call the function anything you like. And what is between the {} braces can be changed to whatever you need.
Write some code for the third function and if it does not work post it here and we can figure out what is wrong. I am happy to help you learn, but I am not here to write your code for you. It makes it harder to learn that way.
I will change the code in my previous message and underline the new additions.
You should also decide in what language you want to program. Is it C or C++?
C source files don’t usually terminate with ‘cpp’ extension.
In C++ you don’t need to use typedef with struct:
1 2 3 4 5 6 7
struct book
{
int bookindex_num;
double bookprice;
}
book book_instance;
In C++ you usually prefer to access struct/class member data by member functions, not by separate functions.