1

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?

5
  • sorry for the format, I don't understand the formatting on this site Commented Apr 30, 2011 at 6:36
  • @J-e-L-Lo : Use {} tag present on the editor window to format the code. Commented Apr 30, 2011 at 6:43
  • 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. Commented Apr 30, 2011 at 6:55
  • @Sriram: Yup, that error is specific to the compiler I believe. Commented Apr 30, 2011 at 7:07
  • Its funny how the g++ command runs the program fine except for displaying the string contents. Commented Apr 30, 2011 at 7:31

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

7 Comments

@Nawaz: That is most definitely a bug, but funnily it compiled on my machine. Also, should this bug result in std::iostream errors?
@Sriram: left-shift is syntactically correct. So it will compile. Also, the std::iostream errors come from somewhere else which is not the code you've quoted here.
? what do you mean not the code. I am compiling the same exact code on my macbook pro 2010 and its giving me those errors.
@J-e-L-L-o: Are you able to compile and run any other program that uses iostream?
thats stupid weird...I changed the left shift operator and it was still not compiling. I saved the cpp file to a different directory and bam it works. Thanx guys.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.