![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
Fahrenheit / Celsius compile problem
I bought the C++ Weekend Crash Course, and when I came across this problem I checked online and apparently this program is in several books.
Note, this is the SAMPLE compile out of the book. It's supposed to work as a demo to learn how to compile a program. Here's the program as it appears in the book: #include <iostream.h>
#include <stdio.h>
int main (int nNumberofArgs, char* pszArgs[]) {
int nCelsius;
cout << "Enter the temperature in Celsius:";
cin > nCelsius;
int nFactor;
nFactor = 212 - 32;
int nFahrenheit;
nFahrenheit = nFactor * nCelsius/100 + 32;
cout << "Fahrenheit value is:";
cout << nFahrenheit;
return 0;
}Problem: upon compile in both Visual C++ 6.0 (Windows) and XCode (Macintosh) (I haven't tried it on my Linux compy yet), the compiler has a problem with the line that reads cin > nCelsius; It's exactly what the book says! What am I doing wrong? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
|
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Yup. That should do the trick by the looks of it. Good luck! Let us know if you have any other problems.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You may need to specify the namespace, either by adding:
using namespace std; std::cout << whatever; std::cin >> whatever;
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
.... and using <iostream> and (optionally) <cstdio> rather than <iostream.h> and <stdio.h> respectively.
<stdio.h> is supported from C++, because of backward compatibility to C. <iostream.h> has never been standard C++; any book that uses it probably dates from days when the C++ standard was an evolving draft. <iostream.h> was quickly superseded by <iostream>. |
|
|
|
|
|
#6 |
|
Newbie
|
It could be syntax error
Give cin >> nCelsius; |
|
|
|
|
|
#7 |
|
Newbie
|
Hi dawai;
I have one doubt. 'cin', 'cout' are c++ key words. why we need name spaces. I think this platform dependent. I have tried in Turbo c++, I didn't get any errors. Can please give me detailed description? regards chakri |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
cin and cout are NOT C++ keywords. They're functions, defined in the iostream header file. You need to tell the compiler you're using the std namespace because that's where they are - in the std namespace.
If you're using iostream.h, stop now. That header file was created before the C++ standard was finalised - it's obsolete now. Use iostream instead, as grumpy suggests, and either prefix all the standard functions with "std::" (this includes things from fstream, etc.), or type using namespace std; after you include your header files. |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Actually, cin and cout are not functions. The are stream objects. The functions are in the insertion and extraction operators << and >>. Those operator functions return (in the absence of error) a reference to the stream, which is why they may be chained together. Since the objects are defined in the std namespace you must either use that namespace or qualify them at the time of use. It seems like a pain in the butt at times, but there's nothing to preclude you from defining your own object and naming it "cin".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|