Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 2nd, 2005, 3:29 PM   #1
andrewgray
Newbie
 
Join Date: Jul 2005
Location: Seattle, WA
Posts: 3
Rep Power: 0 andrewgray is on a distinguished road
Send a message via AIM to andrewgray Send a message via MSN to andrewgray
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;
in the program. Microsoft Visual C++ 6.0 and XCode alike give me the error message "error: no match for 'std::istream& > int&' operator".

It's exactly what the book says! What am I doing wrong?
andrewgray is offline   Reply With Quote
Old Jul 2nd, 2005, 3:31 PM   #2
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
I think it was a typo.
Try
cin >> nCelsius;
i.e. double ">"
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jul 2nd, 2005, 5:44 PM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Yup. That should do the trick by the looks of it. Good luck! Let us know if you have any other problems.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jul 2nd, 2005, 6:41 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You may need to specify the namespace, either by adding:

using namespace std;
below the includes OR by using
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
DaWei is offline   Reply With Quote
Old Jul 2nd, 2005, 10:13 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
.... 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>.
grumpy is offline   Reply With Quote
Old Jul 5th, 2005, 9:45 AM   #6
chanti
Newbie
 
Join Date: Jul 2005
Location: HYDERABAD(INDIA)
Posts: 4
Rep Power: 0 chanti is on a distinguished road
Send a message via Yahoo to chanti
It could be syntax error
Give
cin >> nCelsius;
chanti is offline   Reply With Quote
Old Jul 5th, 2005, 9:49 AM   #7
chanti
Newbie
 
Join Date: Jul 2005
Location: HYDERABAD(INDIA)
Posts: 4
Rep Power: 0 chanti is on a distinguished road
Send a message via Yahoo to chanti
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
chanti is offline   Reply With Quote
Old Jul 5th, 2005, 10:19 AM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jul 5th, 2005, 11:01 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 5th, 2005, 2:14 PM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Whoops... knew I got something wrong. Well, ya learn something new every day, don't ya?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC