Sign in to follow this  
HDRebel88

C++ Tutorials

Recommended Posts

OK HDRebel88, i posted a question in msdn about your problem, and i was told this may help.

http://www.programmerstalk.net/thread1504.html

Let me know how you get on :)

Did what the person did in that link... New error:

1>------ Build started: Project: convert, Configuration: Debug Win32 ------
1>Compiling...
1>convert.cc
1>c:\documents and settings\movie\my documents\visual studio 2008\projects\convert\convert\convert.cc(6) : fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
1>Build log was saved at "file://c:\Documents and Settings\Movie\My Documents\Visual Studio 2008\Projects\convert\convert\Debug\BuildLog.htm"
1>convert - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Tried it without iostream.h, get this:

1>------ Build started: Project: convert, Configuration: Debug Win32 ------
1>Compiling...
1>convert.cc
1>c:\documents and settings\movie\my documents\visual studio 2008\projects\convert\convert\convert.cc(8) : error C2143: syntax error : missing ',' before '.'
1>c:\documents and settings\movie\my documents\visual studio 2008\projects\convert\convert\convert.cc(12) : error C2065: 'cout' : undeclared identifier
1>c:\documents and settings\movie\my documents\visual studio 2008\projects\convert\convert\convert.cc(13) : error C2065: 'cin' : undeclared identifier
1>c:\documents and settings\movie\my documents\visual studio 2008\projects\convert\convert\convert.cc(24) : error C2065: 'cout' : undeclared identifier
1>c:\documents and settings\movie\my documents\visual studio 2008\projects\convert\convert\convert.cc(25) : error C2065: 'cout' : undeclared identifier
1>Build log was saved at "file://c:\Documents and Settings\Movie\My Documents\Visual Studio 2008\Projects\convert\convert\Debug\BuildLog.htm"
1>convert - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites

OK, HDRebel88

Can you revert back to how they were, Ive had another 2 replies that may help.

Nick Meyer - Posted 3 hours 22 minutes ago

This code doesn't give you compile errors? It should. The standard C++ headers ending in ".h" are far outdated. You should use <cstdio> and <iostream> -- although I don't see anything in your code that requires <cstdio>.

In the new headers, cout, cin, endl, etc. are all in the "std" namespace. You should preface each use of those names with "std::".


And
IthilenX - Posted 2 hours 22 minutes ago

In addition to what Nick Meyer said. There's one other thing: int main(int nNumberofArgs. char* pszArgs[])
That looks like a "period" after nNumberofArgs. If so, change to comma.

Share this post


Link to post
Share on other sites
OK, HDRebel88

Can you revert back to how they were, Ive had another 2 replies that may help.



And


I'm just going by what the book has... So if that's wrong, the whole book is wrong.


EDIT: Well I did what they said and it worked.

//

// Program to convert temperature from Celsius degree units into Fahrenheit degree units

// Fahrenheit = Celsius * (212 - 32)/100 + 32

//

#include <cstdio>

#include <iostream>

using namespace std; 


int main(int nNumberofArgs, char* pszArgs[])

{

	//enter the temperature in Celsius

	int celsius;

	cout << "Enter the temperature in Celsius:";

	cin >>celsius;


	//calcuate conversion factor for Celsius to Fahrenheit

	int factor;

	factor = 212 - 32;


	//use conversion factor to convert Celsius into Fahrenheit values

	int fahrenheit;

	fahrenheit = factor * celsius/100 + 32;


	//output the result

	cout << "Fahrenheit value is:";

	cout << fahrenheit;


	return 0;

}



Just don't know how I'm going to use this book now.
Also, when I open the program, I type in a number it computes it and immediately closes. Shouldn't it stay open?

Share this post


Link to post
Share on other sites

You are viewing it from the actual file?

You need to go to command prompt (by running cmd) and then type in the location of the exe file

(Usually what i do is copy the exe file to C drive and give it a name like, in your case, converter, and i just enter the command "C:\converter.exe")

Alternatively just after

//output the result

	cout << "Fahrenheit value is:";

	cout << fahrenheit;



Enter "cin;" which it would then pause the program while it waits for the Enter key to be pressed.

Its the same with every language, for C# its "command.readLine();"

Share this post


Link to post
Share on other sites
You are viewing it from the actual file?

You need to go to command prompt (by running cmd) and then type in the location of the exe file

(Usually what i do is copy the exe file to C drive and give it a name like, in your case, converter, and i just enter the command "C:\converter.exe")

Alternatively just after

//output the result

	cout << "Fahrenheit value is:";

	cout << fahrenheit;

Enter "cin;" which it would then pause the program while it waits for the Enter key to be pressed. Its the same with every language, for C# its "command.readLine();"

I found the updated (5th version of the book) and they have a few pages posted online including the script I was doing from the older (4th) edition. So here's the newer version from the book... which takes into account the dropping of the .h files and using system("PAUSE").

//

// Program to convert temperature from Celsius degree units into Fahrenheit degree units

// Fahrenheit = Celsius * (212 - 32)/100 + 32

//

#include <cstdio>

#include <cstdlib>

#include <iostream>

using namespace std; 


int main(int nNumberofArgs, char* pszArgs[])

{

	//enter the temperature in Celsius

	int celsius;

	cout << "Enter the temperature in Celsius:";

	cin >>celsius;


	//calcuate conversion factor for Celsius to Fahrenheit

	int factor;

	factor = 212 - 32;


	//use conversion factor to convert Celsius into Fahrenheit values

	int fahrenheit;

	fahrenheit = factor * celsius/100 + 32;


	//output the result

	cout << "Fahrenheit value is:";

	cout << fahrenheit << endl;


	//wait until user is ready before terminating program

	//to allow user to see the program results

	system("PAUSE");

	return 0;

}

Share this post


Link to post
Share on other sites
5th version?

What book you reading?


C++ for Dummies.
(http://media.wiley.com/product_data/excerpt/23/07645685/0764568523.pdf)



hmm.... this is interesting.

When I change the code around to use fahrenheit = Celsius * 9/5 + 32 I get two diffent answers depending on how I do it.

If I do:

	int factor;

	factor = 1.8;


	//use conversion factor to convert Celsius into Fahrenheit values

	int fahrenheit;

	fahrenheit =(factor * celsius) + 32;

When typing in 20 for Celsius, I get the answer as 52 (so it's not even taking the "factor" into account). But when I do:

	//use conversion factor to convert Celsius into Fahrenheit values

	int fahrenheit;

	fahrenheit =(1.8 * celsius) + 32;



I get the right answer of 68.

Share this post


Link to post
Share on other sites

I always use this

1.8

x - 32



To go from celsius to farenheight

___

Cool thanks for the link, i might buy a 790 paged book to get me started on it from amazon for £40. I thought it was a bargain!!

Share this post


Link to post
Share on other sites

	int factor;

	factor = 1.8;


	//use conversion factor to convert Celsius into Fahrenheit values

	int fahrenheit;

	fahrenheit =(factor * celsius) + 32;


...you fool.

Let me go through your code line-by-line so you can see what's happening.

int factor;
Declare an integer called factor.
factor = 1.8;
Set factor to 1.8. Since 1.8 isn't an integer, change it to an integer by rounding down to 1.

Understand why it's not working now? :P Edited by Zarel

Share this post


Link to post
Share on other sites
...you fool.

Let' me go through your code line-by-line so you can see what's happening.

int factor;
Declare an integer called factor.
factor = 1.8;
Set factor to 1.8. Since 1.8 isn't an integer, change it to an integer by rounding down to 1.

Understand why it's not working now? :P


lol... okay. I have no clue what half this stuff stands for. How can I use 1.8 in that manner then? Although, the 1.8 * celsius + 32 does provide more compact code. Just wanted to learn both ways of doing it (if there are even two ways).

Share this post


Link to post
Share on other sites
lol... okay. I have no clue what half this stuff stands for. How can I use 1.8 in that manner then? Although, the 1.8 * celsius + 32 does provide more compact code. Just wanted to learn both ways of doing it (if there are even two ways).

Why are you making factor=1.8 a variable but not offset=32? Normally, magic numbers are a bad thing, but for something like this, you might as well hardcode the value. In any case, you should do it like this:

	double factor = 1.8; int offset = 32;


	int fahrenheit = (factor * celsius) + offset;

Well, actually, if you're not going to use magic numbers, you might as well use #defines, which is what they're made for:

#define TEMPCONV_FACTOR 1.8

#define TEMPCONV_OFFSET 32


	int fahrenheit = (TEMPCONV_FACTOR * celsius) + TEMPCONV_OFFSET;

But seriously. In a case like this, you have no reason not to just do:

	int fahrenheit = (1.8 * celsius) + 32;

Edited by Zarel

Share this post


Link to post
Share on other sites
Wait a minute?

Whats the "factor" and "offset"s for? There is only one way to work out f/c and c/f.

Exactly. That's why I recommended just using:

	int fahrenheit = (1.8 * celsius) + 32;

Share this post


Link to post
Share on other sites
Wait a minute?

Whats the "factor" and "offset"s for? There is only one way to work out f/c and c/f.


celsius to fahrenheit is celsius x 9/5 + 32. Factor is just the 9/5 and offset is the 32. They're basically just set variables.

For PHP it be:

<?php

$celsius='form variable';
$factor='1.8';
$offset='32';

echo ($factor * $celsius) + $offset;

?>

I just tend to like to work in variables, but that's because I screw around their values alot. This would be an occasion where having everything in the one line does make more sense though.

Share this post


Link to post
Share on other sites
Cool thanks for the link, i might buy a 790 paged book to get me started on it from amazon for £40. I thought it was a bargain!!


Was that book called "The C++ Programming Language - Special Editon", from Stroustrup?

Share this post


Link to post
Share on other sites
http://www.amazon.co.uk/First-Book-C-Here-...8660&sr=8-1

A First Book of C++, from Here to There (Paperback)


Ahh, I just bought the updated copy of C++ for Dummies and the book I mentioned in my last post. "The C++ Programming Language" Book is actually written by the creator of C++. But alot of the Reviews on the book are that it's written in a way you truely have to read each line a few times to actually understand it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this