Mailinglist Archive: opensuse-programming (91 mails)
| < Previous | Next > |
Re: [suse-programming-e] c++ array problem
- From: alan@xxxxxxxxxxx
- Date: Sat, 03 May 2003 08:31:05 +0100
- Message-id: <3EB37E49.20969.4D3D29E@localhost>
On 2 May 2003 at 8:27, Jerry Feldman wrote:
Date sent: Fri, 2 May 2003 08:27:02 -0400
From: Jerry Feldman <gaf@xxxxxxx>
To: suse-programming-e@xxxxxxxx
Organization: Boston Linux and Unix
Subject: Re: [suse-programming-e] c++ array problem
> On Fri, 2 May 2003 11:50:21 +0000
> Gedi <gedi@xxxxxxxxxxxx> 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 <gaf@xxxxxxx> 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
Date sent: Fri, 2 May 2003 08:27:02 -0400
From: Jerry Feldman <gaf@xxxxxxx>
To: suse-programming-e@xxxxxxxx
Organization: Boston Linux and Unix
Subject: Re: [suse-programming-e] c++ array problem
> On Fri, 2 May 2003 11:50:21 +0000
> Gedi <gedi@xxxxxxxxxxxx> 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 <gaf@xxxxxxx> 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
| < Previous | Next > |