• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ Header files

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Speciale

Disabled
Joined
May 29, 2005
Location
woodstock illinois
I'm trying to slowly teach myself some basic C++ programming. A few years ago i bought C++ for dummies...it was the easiest of the lot to read and so i figured it was a good place to start (all the other books kind of made it seem like you needed some sort of backround).

At any rate it came with a compiler and the programs it has you program worked fine. I lost that disk and now use i'm using C++ visual express, and i've tried another free one from the web.

The very first program:
Code:
//
// Program to convert temperature from Celsius degree
// units into Fahrenheit degree units:
// Fahrenheit = Celsius * (212 - 32)/100 + 32
//

#include <stdio>
#include <iostream>

int main(int nNumberofArgs, char* pszArgs[])
{
	// enter the temperature in Celsius
	int celsius;
	cout << "Enter the temperature in Celsius:";
	cin >> celsius;

	// calculate conversion factor for Celsius
	// To Fahrenheit
	int factor;
	factor = 212 - 32;

	//use conversion factor to convert Celsius
	// into Fahrenheit values
	int fahreneit;
	fahrenheit = factor * celsius/100 + 32;

	// output the results
	cout << "Fahrenheit value is:";
	cout << fahrenheit;

	return 0;
}

When i try and compile it i get this error:

------ Build started: Project: C++, Configuration: Debug Win32 ------
Compiling...
temp conversion.cpp
.\temp conversion.cpp(7) : fatal error C1083: Cannot open include file: 'stdio': No such file or directory
Build log was saved at "file://c:\Documents and Settings\Tim\My Documents\Visual Studio 2005\Projects\C++\C++\Debug\BuildLog.htm"
C++ - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

originally it is suppose to be

Code:
#include <iostream.h> 
#include <stdio.h>

And i get the same error. It's my understanding that the iostream.h and stdio.h define cin and cout...am i right?

I've tried to look them up online and they seem very common, it seems that this should work. Someone want to explain to me where the error is??
 
It should be

Code:
#include <iostream>
using std::cin;
using std::cout;

or

Code:
using namespace std;

for the lazy minded
 
It looks like the sample code was written for an older compiler that doesn't take into account some changes to the standard that have happened (several years ago.)

stdio was required to get main to link correctly by some compilers. With any modern compiler you should be able to leave that line off. Alternatively, you can add the .h, in which case it should be able to find the file (even though you don't need it.)

You should leave the .h off from iostream. If you put .h on it, it will use a version that exists only for compatibility, which may cause you headaches later. Everyone leaves the .h off and its best to do what everyone does.

The old standard didn't use the namespace stuff, but it is required now. This means that in your sample code you will usually have to add the following after you include the header files.
Code:
using namespace std;
Don't worry about what this does just yet. Its a little more advanced than beginner code, you just have to have it.
 
Thanks for the help guys, sure enough that worked. I kind of figured it was something like that since the book is so old. This should let me get through the rest of it i think.
 
Back