How to clear Header File Problem?

my.h
#include <iostream.h>
#include <stdio.h>

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?

Thanks and regards,
Kuluoz
Hello Kuluoz,

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;
}


Hope that helps,

Andy
Thanks for suggestion. i ll correct in next time.

now i want to use values(m_book.bookprice) in thired.cpp.

what i do now?

Regards
Kuluoz
Hello Kuluoz,

You might try this:

My.h
1
2
3
4
5
6
7
8
9
10
#ifndef _MYHEADER 
#define _MYHEADER

typedef struct book
{
	int bookindex_num;
	double bookprice;  // <--- Changed to double.
}mybook;

#endif // end !_MYHEADER 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

#include "My.h"  // <--- Include in any file that needs this header.

//  prototypes
void Print(const struct 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(const struct 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;
}


Hope that helps,

Andy

EDIT: Additions are underlined.
Last edited on
Thanks Andy.
what is my concept?

in header file i was used lot of data(count:30) type.//my.h inside of typedef struct

in cpp file i assign some values for data //my.cpp

in third.cpp file i want to call one data field. and also return values from cpp file

example:

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

understand? i need function for third.cpp.

Kuluoz
Last edited on
Hello Kuluoz,

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.

Hope that helps,

Andy
Thanks. I will check

Kuluoz
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.
Topic archived. No new replies allowed.