Archived

This topic is now archived and is closed to further replies.

Ice-T

Help, very simple C++ code wont work on MSVC.

Recommended Posts

Can somebody tell why this wont work? #include <iostream> int main() { char r = ''r''; cout << r; return(0); } It tells me that cout is unrecognized. Prior to this, it said that and couldn''t be found. If so, where can I get these files? It''s not on my MSVC CD.

Share this post


Link to post
Share on other sites
Thanks for the help but that''s not it. I keep getting this error:

c:\program files\microsoft visual studio\vc98\include\xlocale(11) : fatal error C1083: Cannot open include file: ''stdexcept'': No such file or directory
Error executing cl.exe.

Where can I get the "stdexcept"? I can''t seem to get it off my MSVC CD.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You might want to try putting .h after the file''s name. If namespaces don''t work....

ex.
#include <iostream>
using namespace std;

... this may work out better for you...

#include <iostream.h>
// using namespace std; <-- don''t need this with *.h

Also, if you are working with Visual Studio (esp. 6.0), make sure your project is a Win32 Console Application, not a Win32 Application.

I hope this works out for you.
~Belgarion

Share this post


Link to post
Share on other sites
quote:
Original post by RhoneRanger
char r="r";

should also work.

No it shouldn't. Don't be a dumbass; that's an implicit conversion from "const char*" to "char", which doesn't work in any context. How could that possibly help anything?

[edited by - zealouselixir on August 10, 2003 1:35:36 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I believe that trying to use:

char r = "r";

as opposed to:

char r = ''r'';

will result in an error.

The "r" is a null terminating string, so there is a hidden element, the ''\0'' character. A char type will only accept a single char type, not two. If you turn r into:

char r[2];

Then you might be able to work around that. If I am mistaken, please tell me.

~Belgarion

Share this post


Link to post
Share on other sites

#include <iostream>

int main()
{
char r = 'r';
std::cout << r;
}


If the preceeding code does not work there's something wrong with your libraries and/or install.

[EDIT] What version of MSVC are you using?



[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on August 10, 2003 1:04:51 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Lektrix is right to tell you not to use *.h for your include file names. The *.h is a little thing left over from C and pre-ANSI/ISO, non-standard C++. Namespaces should be learned at an early stage. The committee that standardized C++ declared it good programming style to use namespaces to prevent conflict from different header files and such with the same name. Which is very much the same as the way overloaded functions are differentiated.

Out of the examples:

#include <iostream>
using namespace std;
int main()
{
char r = ''r'';
cout << r;
return(0);
}

... and ...

#include <iostream>
int main()
{
char r = ''r'';
std::cout << r;
return(0);
}

Know both of them (why they work and such), but use the first as extensively as possible. This will keep you code simple and clean. Otherwise (not using namespaces), you could very well end up using std::______ for every statement from iostream or any other include file you decide to use. It gets fairly tiresome (^_^)

I do apologize to anyone who was offended by my usage of *.h. Old habits die hard. (^_^)
~Belgarion

Share this post


Link to post
Share on other sites
Or you could just pull the needed elements into the global namespace, instead of eliminating its purpose by "using namespace std", which effectively aliases std with the global namespace. Like so:

using std::cout;
using std::endl;

Hey, not tiring, and you can prevent global namespace pollution. Sounds like a winner.

Share this post


Link to post
Share on other sites