1
votes

I am a computer science student and taking my first C++ class. I have a problem understanding what is going on with my code:

// This program uses the address of each element in the array. 
#include <iostream>
using namespace std;

int main()
{
    const int NUM_COINS = 5;
    int coins[NUM_COINS] = {5, 1, 25, 5, 10};
    int *p1;        // Pointer to a double.
    int count;                      // Counter variable. 

    // Use the pointer to display the values in the array. 
    cout << "Here are the values in the coins array: \n";
    for(count = 0; count << NUM_COINS; count++)
    {
        // Get the address of an array element
        p1 = &coins[count];

        // Display the contents of the element
        cout << *p1;
    }
    cout << endl;
    return 0;
}
  1. so my first question is why doesn't make compile it? I have no problems at all with any of my other simple programs. I am using g++ on OS X 4.2.1. I have to type the g++ -o command for it to compile, if not...i get these errors:

g++ -c -o 9-8.o 9-8.cpp cc 9-8.o -o 9-8 Undefined symbols: "std::basic_ostream >& std::operator<<

(std::basic_ostream >&, char const*)", referenced from: _main in 9-8.o _main in 9-8.o "std::ios_base::Init::Init()", referenced from: __static_initialization_and_destruction_0(int, int)in 9-8.o
"std::basic_string, std::allocator >::size() const", referenced from: std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o "std::basic_string, std::allocator ::operator[](unsigned long) const", referenced from: std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o "___gxx_personality_v0", referenced from: std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o ___tcf_0 in 9-8.o _main in 9-8.o unsigned long const& std::min(unsigned long const&, unsigned long const&)in 9-8.o __static_initialization_and_destruction_0(int, int)in 9-8.o global constructors keyed to mainin 9-8.o CIE in 9-8.o "std::ios_base::Init::~Init()", referenced from: ___tcf_0 in 9-8.o "std::basic_ostream >& std::endl (std::basic_ostream >&)", referenced from: _main in 9-8.o "std::basic_ostream ::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))", referenced from: _main in 9-8.o "std::basic_ostream ::operator<<(int)", referenced from: _main in 9-8.o "std::cout", referenced from: _main in 9-8.o _main in 9-8.o _main in 9-8.o ld: symbol(s) not found collect2: ld returned 1 exit status make: *** [9-8] Error 1

which leads to my second question. Even if I do type the g++ command, it compiles but after running it outputs an empty array. So my question #2 is: is my code correct? How do I properly use pointers with the reference address statement?

2
sorry for the format, I don't understand the formatting on this siteJ-e-L-L-o
@J-e-L-Lo : Use {} tag present on the editor window to format the code.Mahesh
the left-shift operator in place of the comparison operator is a bug, but i still dont understand why that should give you std::iostream errors. Once you fix that, the rest of the code works ok for me.Sriram
@Sriram: Yup, that error is specific to the compiler I believe.Eric Z
Its funny how the g++ command runs the program fine except for displaying the string contents.J-e-L-L-o

2 Answers

6
votes

Reason: You are not using the comparision operator correctly. After changing it to be "<", your code should work correctly.

for(count = 0; count << NUM_COINS; count++)
                     ^ should be "<" here
2
votes

I don't see any problem except that one problem in your for loop:

for(count = 0; count << NUM_COINS; count++)
                   //^^

That is not comparison. That is left-shift operation. I'm sure you didn't intend that.

That should be : count < NUM_COINS.