Hi all,
I've just started to learn C++ in the last few days and have some accross a small problem.
I am trying to build an array to take a number of inputs and print these inputs to stdout. I have read as much material as I can to try and understand why my small program will not compile to no avail. Can anyone point me in the right direction.
Here is a snippet of the code that is causeing me the problem :
#include
On Fri, 2 May 2003 11:50:21 +0000
Gedi
I've just started to learn C++ in the last few days and have some accross a small problem. Here is a few corrections to your code: #include <iostream> #include <iomanip> using std::cout; using std::cin; using std::endl; int ix, array[10];
int main()
{
for(ix = 0; ix < 10; ix++) {
cout << "Enter string: ";
cin >> array[ix];
}
// cout << array; // a little unsure as to how you print the strings
out as a whole and seperatly
cout << array[1] << endl;
cout << array[3] << endl;
return 0;
}
My changes are:
1. remove the .h for the header files. The older headers are deprecated.
2. cin and cout are in namespace std. You could have used "using
namespace std;" as an alternative.
3. index is a Linux function and declared in string.h which is
implicitly included.
4. Make sure you have line endings. It is best to use endl from the
iomanip header rather than '\n', but '\n' or "\n" woould have worked
fine also.
5. You cannot print an entire array unless the class includes the
appropriate friend functions overloading the << operator. Since int is a
basic type, there is no support for it in the language. In your case,
cout << array << endl; will print out the address of the array.
6. Your "Enter string" comment is a bit of a misnomer. You should ask
for a number since you are entering and int.
7. main should return something.
I suggest you get a good book on C++. Bruce Eckel's Thinking in C++ is
available online:
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
Additionally another book I use is "The C++ Primer".
Several books on C++ have been discussed on this forum, and you can
check the archives.
--
Jerry Feldman
On 2 May 2003 at 8:27, Jerry Feldman wrote:
Date sent: Fri, 2 May 2003 08:27:02 -0400
From: Jerry Feldman
On Fri, 2 May 2003 11:50:21 +0000 Gedi
wrote: I've just started to learn C++ in the last few days and have some accross a small problem. Here is a few corrections to your code: #include <iostream> #include <iomanip> using std::cout; using std::cin; using std::endl; int ix, array[10];
int main() { for(ix = 0; ix < 10; ix++) { cout << "Enter string: "; cin >> array[ix];
}
// cout << array; // a little unsure as to how you print the strings out as a whole and seperatly cout << array[1] << endl; cout << array[3] << endl; return 0; }
My changes are: 1. remove the .h for the header files. The older headers are deprecated. 2. cin and cout are in namespace std. You could have used "using namespace std;" as an alternative. 3. index is a Linux function and declared in string.h which is implicitly included. 4. Make sure you have line endings. It is best to use endl from the iomanip header rather than '\n', but '\n' or "\n" woould have worked fine also. 5. You cannot print an entire array unless the class includes the appropriate friend functions overloading the << operator. Since int is a basic type, there is no support for it in the language. In your case, cout << array << endl; will print out the address of the array. 6. Your "Enter string" comment is a bit of a misnomer. You should ask for a number since you are entering and int. 7. main should return something.
I suggest you get a good book on C++. Bruce Eckel's Thinking in C++ is available online: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html Additionally another book I use is "The C++ Primer". Several books on C++ have been discussed on this forum, and you can check the archives. -- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
C++ Primer is very good - I recommend it too. Also, in addition to Jerry's comments you might like to consider that you should probably be using a vector container, rather than an array... alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
On Sat, 03 May 2003 08:31:05 +0100 alan@ibgames.com wrote:
C++ Primer is very good - I recommend it too. Also, in addition to Jerry's comments you might like to consider that you should probably be using a vector container, rather than an array... I would agree with this if this was a real problem. But, Gedi is just learning C++, and probably should learn the basics before using the features of the STL. -- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Saturday 03 May 2003 07:11, Jerry Feldman wrote:
On Sat, 03 May 2003 08:31:05 +0100
alan@ibgames.com wrote:
C++ Primer is very good - I recommend it too. Also, in addition to Jerry's comments you might like to consider that you should probably be using a vector container, rather than an array...
I would agree with this if this was a real problem. But, Gedi is just learning C++, and probably should learn the basics before using the features of the STL. =====================
Not to hijack a thread and I don't remember if this was mentioned earlier, but who are the authors of the C++ Primer and what edition? I did find some hits when doing a search, but there seems to be more than one listed all with different authors. I know Jerry has been involved with C & C++ for a while now, so I thought he would know exactly. Thanks Patrick -- --- KMail v1.5.9.1i --- SuSE Linux Pro v8.2 --- Registered Linux User #225206 On any other day, that might seem strange...
On Sat, 3 May 2003 10:33:25 -0400
"O'Smith"
Not to hijack a thread and I don't remember if this was mentioned earlier, but who are the authors of the C++ Primer and what edition? I did find some hits when doing a search, but there seems to be more than one listed all with different authors. I know Jerry has been involved with C & C++ for a while now, so I thought he would know exactly. Stanley B. Lippman and Josee Lajoie Publisher: Addison Wesley. The latest edition appears to be the Third Edition. -- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On 3 May 2003 at 12:06, Jerry Feldman wrote:
Date sent: Sat, 3 May 2003 12:06:47 -0400
From: Jerry Feldman
On Sat, 3 May 2003 10:33:25 -0400 "O'Smith"
wrote: Not to hijack a thread and I don't remember if this was mentioned earlier, but who are the authors of the C++ Primer and what edition? I did find some hits when doing a search, but there seems to be more than one listed all with different authors. I know Jerry has been involved with C & C++ for a while now, so I thought he would know exactly. Stanley B. Lippman and Josee Lajoie Publisher: Addison Wesley. The latest edition appears to be the Third Edition. -- Jerry Feldman
The third edition is the most recent published. Stan has just started work on the fourth edition, but I would guess it will be a while before it's out. alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
On 3 May 2003 at 7:11, Jerry Feldman wrote:
Date sent: Sat, 3 May 2003 07:11:31 -0400
From: Jerry Feldman
On Sat, 03 May 2003 08:31:05 +0100 alan@ibgames.com wrote:
C++ Primer is very good - I recommend it too. Also, in addition to Jerry's comments you might like to consider that you should probably be using a vector container, rather than an array... I would agree with this if this was a real problem. But, Gedi is just learning C++, and probably should learn the basics before using the features of the STL. -- Jerry Feldman
Ah! The eternal discussion - when should the STL be introduced to people learning C++ :) alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
On Saturday 03 May 2003 22:01, alan@ibgames.com wrote:
Ah! The eternal discussion - when should the STL be introduced to people learning C++ :)
I think NOW would be perfect. Here are yet a few improvements to Feldman's implementation: #include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() { using std::vector; using std::cout; using std::flush; using std::cin; using std::copy; using std::ostream_iterator; vector<int> array(10); for (int i = 0; i < 10; ++i) { cout << "Enter number: " << flush; cin >> array[i]; } copy(array.begin(), array.end(), ostream_iterator<int>(cout, "\n")); } 1. Yes, vector<> should be used in favor of C style arrays 2. <iomanip> is not needed for std::endl or std::flush 3. '\n' is preferred over std::endl, if stream flushing is not desired 4. (I think) cout is flushed upon destruction, i.e., at program exit 5. 'using' directives should be avoided in global scope 6. global variables should generally be avoided 7. main() returns 0 if not otherwise stated 8. I'm not intending to shine with my implementation, but I do certainly think that the standard library should be studied from the beginning, together with the rest of the language. C++ is not simply 'C with classes'. It is a complete language of its own, with a very powerful standardized library. 9. Besides Lippman-Lajoie, I'd like to push for the following titles: - Stroustrup: The C++ Programming Language, 3rd Edition, Addison-Wesley - Josuttis: The C++ Standard Library, Addison-Wesley -- Ch
On 3 May 2003, Jerry Feldman wrote
One needs to have a good foundation in a language before using things like the STL. But, it is important to understand the STL also. Vectors, trees, stacks and many other template classes you can build on.
One can say the same thing about arithmetic. You should learn to add, subtract, divide and multiply before you use a calculator. -- Jerry Feldman
On 3 May 2003 at 22:48, Christian Andersson wrote: [snip...]
8. I'm not intending to shine with my implementation, but I do certainly think that the standard library should be studied from the beginning, together with the rest of the language. C++ is not simply 'C with classes'. It is a complete language of its own, with a very powerful standardized library.
9. Besides Lippman-Lajoie, I'd like to push for the following titles:
- Stroustrup: The C++ Programming Language, 3rd Edition, Addison-Wesley - Josuttis: The C++ Standard Library, Addison-Wesley
-- Ch
I know there is a lot of disagreement over this issue! I personally feel that the STL should be taught as soon as possible. Most of the more recent books for new C++ programmers take this approach too - for example Koenig & Moo's 'Accelerated C++'. I think it's important to teach people not to re-invent the wheel at an early stage :) The Stroustrup book is good - it has a slightly different slant to Lippman- Lajoie. I actually use both of them. If I get stuck I look it up in one and if I'm still having difficulty I then look it up in the other one. The different styles of explanation usually mean that one of the two is easier to understand on that issue. I don't think Nico Josuttis's book is a 'lerners' book. It is however a superb reference book for the STL - if you are going to get a book on the STL, it is the 'must have' one. Btw, a good place to check out books is http://www.accu.org/bookreviews/public/index.htm All the reviews are done by programmers. Hmmm... I hope people don't think this is hijacking the thread :) alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
On Sun, 04 May 2003 07:23:00 +0100 alan@ibgames.com wrote:
Hmmm... I hope people don't think this is hijacking the thread :) No. I for one appreciate the feedback.
--
Jerry Feldman
On Sun, 04 May 2003 07:23:00 +0100 alan@ibgames.com wrote:
Hmmm... I hope people don't think this is hijacking the thread :) No. I for one appreciate the feedback.
I agree. I appreciate all feedback. Thanks for information provided by all.
Ah! The eternal discussion - when should the STL be introduced to people learning C++ :) One needs to have a good foundation in a language before using things
On Sat, 03 May 2003 21:01:26 +0100
alan@ibgames.com wrote:
like the STL. But, it is important to understand the STL also. Vectors,
trees, stacks and many other template classes you can build on.
One can say the same thing about arithmetic. You should learn to add,
subtract, divide and multiply before you use a calculator.
--
Jerry Feldman
Hi Jerry, thanks for the info.
The books are ordered and should be here soon.
In addition to your recommendation I also bought Practical C++
programming.
I am a great fan of O'Reilly products.
Another quick question for anyone before the books arrive, how would you
get the array to take strings. I obviously changed from int to char, but
instead of holding full words, it holds the characters of each word.
Thanks
-------------------------------------------------------
On Fri, 2 May 2003 11:50:21 +0000
Gedi
I've just started to learn C++ in the last few days and have some accross a small problem. Here is a few corrections to your code: #include <iostream> #include <iomanip> using std::cout; using std::cin; using std::endl; int ix, array[10];
int main()
{
for(ix = 0; ix < 10; ix++) {
cout << "Enter string: ";
cin >> array[ix];
}
// cout << array; // a little unsure as to how you print the strings
out as a whole and seperatly
cout << array[1] << endl;
cout << array[3] << endl;
return 0;
}
My changes are:
1. remove the .h for the header files. The older headers are deprecated.
2. cin and cout are in namespace std. You could have used "using
namespace std;" as an alternative.
3. index is a Linux function and declared in string.h which is
implicitly included.
4. Make sure you have line endings. It is best to use endl from the
iomanip header rather than '\n', but '\n' or "\n" woould have worked
fine also.
5. You cannot print an entire array unless the class includes the
appropriate friend functions overloading the << operator. Since int is a
basic type, there is no support for it in the language. In your case,
cout << array << endl; will print out the address of the array.
6. Your "Enter string" comment is a bit of a misnomer. You should ask
for a number since you are entering and int.
7. main should return something.
I suggest you get a good book on C++. Bruce Eckel's Thinking in C++ is
available online:
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
Additionally another book I use is "The C++ Primer".
Several books on C++ have been discussed on this forum, and you can
check the archives.
--
Jerry Feldman
On Sat, 3 May 2003 16:46:05 +0100
"Gedi"
Another quick question for anyone before the books arrive, how would you get the array to take strings. I obviously changed from int to char, but instead of holding full words, it holds the characters of each word. I have Practical C++ Programming, but have not had time to read it yet.
There is a C++ string type in the standard library. You can crwate an
array of these.
Just add #include <string> and using std::string;
And change int array[10] to string array[10]
The rest of the code with my corrections should work fine.
However, I think that you might want to look more toward creating
classes.
--
Jerry Feldman
participants (5)
-
alan@ibgames.com
-
Christian Andersson
-
Gedi
-
Jerry Feldman
-
O'Smith