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