LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   First C++ program! (https://www.linuxquestions.org/questions/programming-9/first-c-program-223070/)

mrblack 08-27-2004 09:29 AM

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>
#include <stdlib.h>

void main(void)
{
unsigned short int guess, answer = 32, tries = 1;
 cout << "You have 5 tries to reach the correct answer.\n\n";
 cout << "First try:\t\t";
 cin >> guess;
 while ((guess != answer) && (tries < 5)) {
  tries++;
  if (guess < answer) {
  cout << "Higher! Try No. " << tries << "\t";
  } else {
  cout << "Lower! Try No. " << tries << "\t";
  }
 cin >> guess;
 }
 if (guess == answer) {
  cout << "\n\tYou win!\n\t--------\n\n";
 } else {
  cout << "\n\tYou lose!\n\t---------\n\n";
 }
 system("PAUSE");
}


rjlee 08-27-2004 10:46 AM

The last command
Code:

system("PAUSE")
isn't very portable.

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.

Stor_G 08-27-2004 10:53 AM

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

tamtam 08-27-2004 11:34 AM

Quote:

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
Since when did the C standard implement with the iostream library. The program is C++. Most people just starting out on any OO language write basic programs which do not encapsulate OO principles to start with. You have to learn to walk before you run.


Tam

Stor_G 08-27-2004 01:22 PM

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.

deiussum 08-27-2004 01:34 PM

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.

DanTheKiwi 08-28-2004 12:34 AM

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).

Stor_G 08-28-2004 04:36 AM

Quote:

It is not C. At all
Quote:

what i meant is that the program he wrote is in C.
of course a C compiler won't compile this because he included a C++ library.
remove cin/cout and use scanf/printf and the program will work well in C.

DanTheKiwi 08-28-2004 08:57 AM

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 :)

Stor_G 08-28-2004 09:06 AM

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.

1337 Twinkie 08-28-2004 11:07 AM

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.

mrblack 08-28-2004 01:59 PM

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.