Newbie about "clean" c++

G

George

Hello to everybody.....
I saw in my new book (C++ for Dummies, 7 Books in 1, by Jeff Cogswell-2002)
the following code (wich compiles perfectly with the Dev-C++ compiler
included in the books CD-Rom):

#include <iostream>
#include <stdlib.h>
#include <string>

int main()
{
string mystring;
mystring="Hello...";
cout << mystring << endl;

system("PAUSE");

return 0;
}

After a little search i figured out what
the 3rd include line talks about....(Standard Template Library).
But when i tried to compile this code with my
Borland C++ Compiler 5.5.1 i got a lot of errors (as i expected).
Does'nt my compiler know anything about the STL ???
Is it safe and portable to write such programms?
I spended months of learning anything about manipulating
"strings" in c/c++ using pointers and all this stuff.
I did'nt know that there was something so simple......
I'm realy confused about what is "clean" C++ and whats not....
Can anybody help or guide me.......
Many thanks in advance,
George.
 
R

Ron Natalie

George said:
Hello to everybody.....
I saw in my new book (C++ for Dummies, 7 Books in 1, by Jeff Cogswell-2002)
the following code (wich compiles perfectly with the Dev-C++ compiler
included in the books CD-Rom):

I can't tell you anything about the particular compilers you are using.
As far as the program goes there are only two problems.

1. It uses entities from the std namespace without either explicit
qualification or a using directive. Adding
using namespace std;
after the #includes will fix that.

2. What system does with its argument is obviously system specific.
PAUSE may or may not be a valid command (and what it does
may be different) on your system.
 
C

Christof Krueger

George said:
Hello to everybody.....
I saw in my new book (C++ for Dummies, 7 Books in 1, by Jeff Cogswell-2002)
the following code (wich compiles perfectly with the Dev-C++ compiler
included in the books CD-Rom):

#include <iostream>
#include <stdlib.h>
#include <string>

int main()
{
string mystring;
mystring="Hello...";
cout << mystring << endl;

system("PAUSE");

return 0;
}
This example is not "clean" c++, because it includes system-specific
code (the system("PAUSE") call).
After a little search i figured out what
the 3rd include line talks about....(Standard Template Library).
But when i tried to compile this code with my
Borland C++ Compiler 5.5.1 i got a lot of errors (as i expected).
Does'nt my compiler know anything about the STL ???
I'm quite sure that your compiler knows about STL but the problem is
elsewhere.

Try the following:

#include <iostream>
#include <string>

using namespace std; //you need this if you want to
//use endl instead od std::endl
//(and that's what you actually do)

int main()
{
string mystring;
mystring="Hello...";
cout << mystring << endl;

return 0;
}

This does not include the system call, but at least it is "pure" valid
C++ code.
It's not good for a book to use such examples, since the name of the
book is "C++ for Dummies" and not "C++ with compiler xyz on platform abc
for Dummies".
You should get a better C++ book. Search the Newsgroup for book
recommendations.
 
D

Derek Baker

Christof Krueger said:
This example is not "clean" c++, because it includes system-specific
code (the system("PAUSE") call).

I'm quite sure that your compiler knows about STL but the problem is
elsewhere.

Try the following:

#include <iostream>
#include <string>

using namespace std; //you need this if you want to
//use endl instead od std::endl
//(and that's what you actually do)

int main()
{
string mystring;
mystring="Hello...";
cout << mystring << endl;

return 0;
}

This does not include the system call, but at least it is "pure" valid
C++ code.
It's not good for a book to use such examples, since the name of the
book is "C++ for Dummies" and not "C++ with compiler xyz on platform abc
for Dummies".
You should get a better C++ book. Search the Newsgroup for book
recommendations.

You code may be better, but he isn't going to see anything on Dev-C++: you
need something to keep the console window open.
 
C

Christof Krueger

Derek said:
You code may be better, but he isn't going to see anything on Dev-C++: you
need something to keep the console window open.
The simplest way to keep the console window open would be to use the
following code:

[snip]
char tmp;
cin >> tmp;
return 0;
[/snip]

But he was asking about "clean" c++, so I didn't care about things like
keeping console windows open :)
The above code comforts with standard c++, the code used by the "c++ for
dummies" example did not.
 
G

George

Thank you a lot for the answers,but ...forget about the line:
system("PAUSE");
my main problem was:
#include <string>
i wanted to know if this is "clean" C++
I've never seen it before......Thanks again....
 
C

Christof Krueger

George said:
Thank you a lot for the answers,but ...forget about the line:
system("PAUSE");
my main problem was:
#include <string>
i wanted to know if this is "clean" C++
I've never seen it before......Thanks again....

string is part of the STL.
you can see that it is part of the c++ standard because there is no .h
suffix in the header file name.
Using string is way better than using char*, by the way.
 
J

Jeff Schwab

George said:
Thank you a lot for the answers,but ...forget about the line:
system("PAUSE");
my main problem was:
#include <string>
i wanted to know if this is "clean" C++
I've never seen it before......Thanks again....


It's clean, unless you have to use a very old compilers or library
implementation.
 
B

Buster

You code may be better, but he isn't going to see anything on Dev-C++: you
need something to keep the console window open.

The best way to run a 'console-mode' program is from the command line.

Regards,
Buster.
 
D

Derek Baker

Buster said:
The best way to run a 'console-mode' program is from the command line.

Regards,
Buster.

Maybe, but I bet not may people using Dev-Cpp do that. I'm a regular at the
Dev-Cpp forum, and let me tell you "why can't I see my program's output"(and
variations) is the number one question there from newbies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top