C++ OK? Compiler or code error?
Moderator: Forum Moderators
C++ OK? Compiler or code error?
A while back, I bought a book to learn cpp. I got a free compiler from the internet for windows, but later I found I had a free compiler already in linux.
So, I thought "Linux is good -- Better than Win98!". I booted up the compiler. Half an hour later, I could still not figure out how to make a new document, or "project". I looked in the help manual. It said "This section has yet to be written". So, I gave up with linux, figuring that my Win98 "Dev-Cpp" (Version 4.9.8.0) editor would work better. It did, but...
My problem it thus: does the code,, work on a different cpp editor? It it my editor, or my code, that is faulty? I narrowed it down to: , , and .
Should I replace my book or my editor?
Or, is there a different way of putting the code that will do the same thing?
My Hello World program worked, though...
So, I thought "Linux is good -- Better than Win98!". I booted up the compiler. Half an hour later, I could still not figure out how to make a new document, or "project". I looked in the help manual. It said "This section has yet to be written". So, I gave up with linux, figuring that my Win98 "Dev-Cpp" (Version 4.9.8.0) editor would work better. It did, but...
My problem it thus: does the code,
Code: Select all
#include <iostream>
int main()
{
std::cout << "Here is 5: " << 5 << "\n";
std::cout << "The manipulator std::endl ";
std::cout << "writes a new line to the screen.";
std::cout << std::endl
std::cout << "Here is a very big number:\t" << 70000;
std::cout << std::endl
std::cout << "Here is the sum of 8+5:\t";
std::cout << 8+5 << std::endl
std::cout << "Here's a fraction:\t\t";
std::cout << (float)<< 5/8 std::endl
std::cout << "And a very, very big number:\t";
std::cout << (double) 7000 * 7000 << std::endl;
std::cout << "DDR is a ";
std::cout << "C++ programmer!";
char response;
std::cin >>response;
return 0;
}
Code: Select all
std::cout << std::endl
Code: Select all
std::cout << 8+5 << std::endl
Code: Select all
std::cout << (float)<< 5/8 std::endl
Should I replace my book or my editor?
Or, is there a different way of putting the code that will do the same thing?
My Hello World program worked, though...
-
- Posts: 161
- Joined: September 6th, 2005, 10:41 pm
- Location: Australia
You were not wrong.DDR wrote:Linux is good -- Better than Win98!
As far as I can tell, it seems the problems were caused by a few typos(?), mainly missing ";" at the end of a statement. The following should compile fine:
Code: Select all
#include <iostream>
int main()
{
std::cout << "Here is 5: " << 5 << "\n";
std::cout << "The manipulator std::endl ";
std::cout << "writes a new line to the screen.";
std::cout << std::endl;
std::cout << "Here is a very big number:\t" << 70000;
std::cout << std::endl;
std::cout << "Here is the sum of 8+5:\t";
std::cout << 8+5 << std::endl;
std::cout << "Here's a fraction:\t\t";
std::cout << (float) 5/8 << std::endl;
std::cout << "And a very, very big number:\t";
std::cout << (double) 7000 * 7000 << std::endl;
std::cout << "DDR is a ";
std::cout << "C++ programmer!";
char response;
std::cin >>response;
return 0;
}
The "::" operator is used to resolve namespaces in C++. As "cout", "endl" and "cin" are all part of the "std" namespace, it is required here.Herduk wrote:[...] but i don't remember any "std::" Try to remove it.
Best of luck with the learning.
The missing ; are obvious errors, every line needs to be terminated with one unless your command really expands into the next.
std::cout << (float) << ... is also nonsense. (TYPE) is the C style casting and here you're trying the output from casting ... well nothing. Note that a good book would probably rather use
std::cout << static_cast<float>(5 / << std::endl;
which would be the C++ style of casting.
std::cout << (float) << ... is also nonsense. (TYPE) is the C style casting and here you're trying the output from casting ... well nothing. Note that a good book would probably rather use
std::cout << static_cast<float>(5 / << std::endl;
which would be the C++ style of casting.
WesCamp-i18n - Translations for User Campaigns:
http://www.wesnoth.org/wiki/WesCamp
Translators for all languages required: contact me. No geek skills required!
http://www.wesnoth.org/wiki/WesCamp
Translators for all languages required: contact me. No geek skills required!
You must be thinking about C. In C++ you must call standard library stuff from the std namespace unless you're:Herduk wrote:I got only old school memories about C++ (i've to get it again ), but i don't remember any "std::"
Try to remove it.
And for linux, what compiler you have?
i suggest you to get Eclipse and then follow the istruction for work it out with c++ too
using namespace std;
Which is not recommended anyways, as I found in the Wesnoth Wiki yesterday.
Author of the unofficial UtBS sequels Invasion from the Unknown and After the Storm.
It Works!
Thank you all! WildPenguin, your ; fix worked! On to the next chapter!
Hes not necessarily thinking of C. Before the latest C++ standard revision it was common that standard thingies (functions etc.) were introduced in "global namespace". With that old style also header files included were #include<iostream.h> not #include<iostream>.Shadow Master wrote:You must be thinking about C. In C++ you must call standard library stuff from the std namespace unless you're:Herduk wrote:I got only old school memories about C++ (i've to get it again ), but i don't remember any "std::"
Try to remove it.
And for linux, what compiler you have?
i suggest you to get Eclipse and then follow the istruction for work it out with c++ too
using namespace std;
Which is not recommended anyways, as I found in the Wesnoth Wiki yesterday.
And what comes to Linux Cpp compilers, you really do not NEED an IDE. (Integrated Development Environment) for code writing. You can simply write your code with your favourite text editor, and later compile it from commandline. IE if you have code written in mycode.cpp , you can compile it with command:
g++ -Wall -o myprogram mycode.cpp
Or if you have code in many files:
g++ -Wall -c mycode.cpp
g++ -Wall -c morecode.cpp
g++ -o myprogram mycode.o morecode.o
(Assuming you have gcc installed - which usually is installed.
I also suggest you to read about Makefiles. Those ease your life a lot
There's also a decent C++ IDE for linux (eclipse with C++ plugin). I personally dislike eclipse though, since it is not too user friendly (my personal opinion). However debugging your code using eclipse is easier than with plaing gdb. (And if you wish to write C / C++, then you definitely should introduce concept of a debugger to yourself )
CWF-Freeware
There is only 10 types of people, Those who understand binary, and those who don't.
My C blogs:
C - Suomeksi
C Programmer's diary
There is only 10 types of people, Those who understand binary, and those who don't.
My C blogs:
C - Suomeksi
C Programmer's diary
(Oh, and I did not mean to step on anyones toes :/ )
CWF-Freeware
There is only 10 types of people, Those who understand binary, and those who don't.
My C blogs:
C - Suomeksi
C Programmer's diary
There is only 10 types of people, Those who understand binary, and those who don't.
My C blogs:
C - Suomeksi
C Programmer's diary
Yep, that's right at least for Turbo C++. The only C++ libraries I have access to are Microsoft's for Visual C++ 7 and 9, GNU's libstdc++ 6, DJGPP's (which I haven't examined in detail) and Turbo C++'s.Mazzie wrote:Hes not necessarily thinking of C. Before the latest C++ standard revision it was common that standard thingies (functions etc.) were introduced in "global namespace". With that old style also header files included were #include<iostream.h> not #include<iostream>.
Of the ones I have used in detail (MS VC++ 9's and GNU's libstdc++ 6), there are warnings everywhere for those who use the old headers.
I've been using Eclipse CDT since discovering that Eclipse could be convertted into a C++ development environment. I was using KDE's text editor (Kate) for plain code editing and the text terminal for makeing with a bash script. I still don't learn the way makefiles work.And what comes to Linux Cpp compilers, you really do not NEED an IDE. (Integrated Development Environment) for code writing. You can simply write your code with your favourite text editor, and later compile it from commandline. IE if you have code written in mycode.cpp , you can compile it with command:
g++ -Wall -o myprogram mycode.cpp
Or if you have code in many files:
g++ -Wall -c mycode.cpp
g++ -Wall -c morecode.cpp
g++ -o myprogram mycode.o morecode.o
(Assuming you have gcc installed - which usually is installed.
I also suggest you to read about Makefiles. Those ease your life a lot
There's also a decent C++ IDE for linux (eclipse with C++ plugin). I personally dislike eclipse though, since it is not too user friendly (my personal opinion). However debugging your code using eclipse is easier than with plaing gdb. (And if you wish to write C / C++, then you definitely should introduce concept of a debugger to yourself )
But I would really like to know how I can integrate autoconf and automake into my project, as it has grown somewhat big and I need to make it easy for distributing to those who don't have /home/shadowm as their home directories. Also to make it easier the customization of config.h via autogen.sh/configure.
Eclipse's not user friendly, indeed. That was the headache of my classmates in that awful Java course.
Author of the unofficial UtBS sequels Invasion from the Unknown and After the Storm.
I have struggled with automake myself... And I even managed to do a configure scripts for one of my projects... But - how to put it nicely - It is a MESS. If you do not need really lot of configuration, then I really suggest you to use some scripting language to handle the configs... And if you can find some good and simple (and not 500 pages long) tutorial about GNU autotools, then please share this information I remember how frustrated I was when I tried to understand how all those tools were supposed to work :rolleyes:
Since we now seem to be listing tools related to C / C++ coding, I cannot leave doxygen out of this. If you ever take part in larger project, DEMAND doxygen tags to be used. It of course is irritating to add those tags, but it is 1000 times more irritating to try to find out what someone tried to do an year ago... No soursecode will ever be 'ready'. Demands change, computers change, operating systems change, bugs are being fixed (and new bugs produced...) In this reality good documentation is invaluable.
Since we now seem to be listing tools related to C / C++ coding, I cannot leave doxygen out of this. If you ever take part in larger project, DEMAND doxygen tags to be used. It of course is irritating to add those tags, but it is 1000 times more irritating to try to find out what someone tried to do an year ago... No soursecode will ever be 'ready'. Demands change, computers change, operating systems change, bugs are being fixed (and new bugs produced...) In this reality good documentation is invaluable.
CWF-Freeware
There is only 10 types of people, Those who understand binary, and those who don't.
My C blogs:
C - Suomeksi
C Programmer's diary
There is only 10 types of people, Those who understand binary, and those who don't.
My C blogs:
C - Suomeksi
C Programmer's diary