LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2004, 09:29 AM   #1
mrblack
LQ Newbie
 
Registered: Aug 2004
Posts: 2

Rep: Reputation: 0
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");
}
 
Old 08-27-2004, 10:46 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
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.
 
Old 08-27-2004, 10:53 AM   #3
Stor_G
Member
 
Registered: Aug 2004
Posts: 50

Rep: Reputation: 15
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
 
Old 08-27-2004, 11:34 AM   #4
tamtam
Member
 
Registered: May 2004
Distribution: Slackware.
Posts: 323

Rep: Reputation: 33
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

Last edited by tamtam; 08-27-2004 at 11:36 AM.
 
Old 08-27-2004, 01:22 PM   #5
Stor_G
Member
 
Registered: Aug 2004
Posts: 50

Rep: Reputation: 15
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.
 
Old 08-27-2004, 01:34 PM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.
 
Old 08-28-2004, 12:34 AM   #7
DanTheKiwi
LQ Newbie
 
Registered: Aug 2004
Location: Sydney, Australia
Distribution: FreeBSD, Fedora Core 2
Posts: 20

Rep: Reputation: 0
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).
 
Old 08-28-2004, 04:36 AM   #8
Stor_G
Member
 
Registered: Aug 2004
Posts: 50

Rep: Reputation: 15
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.
 
Old 08-28-2004, 08:57 AM   #9
DanTheKiwi
LQ Newbie
 
Registered: Aug 2004
Location: Sydney, Australia
Distribution: FreeBSD, Fedora Core 2
Posts: 20

Rep: Reputation: 0
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
 
Old 08-28-2004, 09:06 AM   #10
Stor_G
Member
 
Registered: Aug 2004
Posts: 50

Rep: Reputation: 15
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.
 
Old 08-28-2004, 11:07 AM   #11
1337 Twinkie
Member
 
Registered: Jul 2004
Distribution: Fedora Core 2
Posts: 79

Rep: Reputation: 15
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.
 
Old 08-28-2004, 01:59 PM   #12
mrblack
LQ Newbie
 
Registered: Aug 2004
Posts: 2

Original Poster
Rep: Reputation: 0
I didnt mean to spark up a debate. Thanks for the feedback guys, much appreciated. Im looking into some stuff now.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sms program and database in linux web program in windows.. does not see each other.. keikun_naruchan Programming 0 07-06-2005 01:40 AM
Total recovery: Which program? ghost4linux, YaST2? Best drive imaging program? lagu2653 Linux - Software 1 06-20-2005 01:44 PM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
Gtk-Warning but program still works... I close konsole, program closes Laptop2250 Linux - Software 2 11-14-2003 11:18 PM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration