I was given some code by my Professor and I had to debug it. I finally found the problem, but I have no idea why it is a problem. Could someone please enlighten me.

Here is the piece of the code that has been corrected:

.
.
.
//Using negative subscripts
	cout<<"\nUsing negative subscripts"<<endl;
	p = a + n-1;
	for (size_t i =0; i < n ; ++i)
		cout << p[-i] << ' ';
		cout<< endl;

Here is the original code piece with the error:

.
.
.
//Using negative subscripts
	cout<<"\nUsing negative subscripts"<<endl;
	p = a + n-1;
	for (size_t i =0; i < n ; ++i)
		cout << p[-i] << '  ';     //   <------ There is an extra space.
		cout<< endl;

Why does it display what it does, instead of just add two spaces between each number? :-/

Recommended Answers

All 3 Replies

..aren't single inverted commas just for char variables? Maybe that would cause a problem because the compiler would be expecting one character and receives two. Just a guess :-/

that is the problem

cout << 'a';  //  good
cout << ' ';  //  good
cout << 'ab';  //  bad
cout << '  ';  // bad

OK, you found the compile time error.

Now, there's still a potentially fatal logical error to be corrected.

commented: Heh. +29
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.