First C++ program!
Hello all;
I have decided to get back into C++. :) I am finding things pretty easy, as I took the dive into learning PHP a little while back. I dont know that much... variables, if's, while's... tell me what you think, and how I can improve it. :) Code:
#include <iostream.h> |
The last command
Code:
system("PAUSE") Are you by any chance using something like Borland Turbo C++, where the output window closes as soon as the program has run? In which case, I'd use getch(); or something like cin << c (where c is a char). That will work even on operating systems that don't have a PAUSE command on the path. |
well, the program you wrote is in C not C++.
but that's a good start. in C++ the concept is quite different (object oriented) rather than C (procedural.) you might want to learn first the concepts of Object Oriented Programming (OOP) using a book, ebook, or just googling for it, unless you want to work in C in which case forget what i said :D |
Quote:
Tam |
Hey,
I don't want to run into fights or arguements here, but what i meant is that the program he wrote is in C. the fact that he used a C++ library is a different story. but the code he himself wrote was standard C. |
One other minor comment... iostream.h and all the standard C/C++ headers with the .h extension are deprecated in the ANSI/ISO C++ spec. Instead, you should include the file w/o the .h, which will put everything from that header into the std namespace. Standard C files should be included by pre-pending a c, and dropping the .h. (e.g. #include <iostream.h> becomes #include <iostream>, #include <stdio.h> becomes #include <cstdio>, etc.)
By doing this you then need to be sure to use the standard C++ stuff from the std namespace. The easiest way to do this is to simply add the following line to your code someplace after your #includes: using namespace std; Or you can change all your couts to std::cout, cins to std::cin, etc. |
bash-2.05b$ gcc lq.c -o lq
lq.c:1:22: iostream.h: No such file or directory lq.c: In function `main': lq.c:8: error: `cout' undeclared (first use in this function) lq.c:8: error: (Each undeclared identifier is reported only once lq.c:8: error: for each function it appears in.) lq.c:10: error: `cin' undeclared (first use in this function) lq.c:5: warning: return type of `main' is not `int' It is not C. At all. (I added char c; and cin << c; in the source code and removed the system("PAUSE"); bit in the code i tried to compile as C). |
Quote:
Quote:
remove cin/cout and use scanf/printf and the program will work well in C. |
I don't understand how it is C. If a program written in java doesnt make use of its OO properties, what is it? What about perl or php? He wrote it as C++, he used C++ libraries, he compiled it with a C++ compiler. So it's C++, as far as i'm concerned.
MrBlack: A cool game to write in a new language to play with strings etc is hangman, i had a bit of fun doing that one :). Give it a go :) |
look, this aint the place to argue about such a trivial matter.
pm me and i'll be sure to talk to you about it till one of us is convienced. |
First, I can't compile it using g++, so I suppose portability is the first thing to fix. Also, I would ditch the Pause command, since not all systems can do that. Try using the ncurses library and the getch() function.
DanTheKiwi mentioned Hangman as a cool game to write in C++. Something that would probably prove helpful is the ncurses library I mentioned earlier. It probably came with whatever compiler you have, and you can find information on it here. |
I didnt mean to spark up a debate. :) Thanks for the feedback guys, much appreciated. Im looking into some stuff now.
|
All times are GMT -5. The time now is 03:50 AM. |