943,670 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 482
  • C++ RSS
Sep 20th, 2009
0

Question about extra whitespace

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. .
  2. .
  3. .
  4. //Using negative subscripts
  5. cout<<"\nUsing negative subscripts"<<endl;
  6. p = a + n-1;
  7. for (size_t i =0; i < n ; ++i)
  8. cout << p[-i] << ' ';
  9. cout<< endl;

Here is the original code piece with the error:
C++ Syntax (Toggle Plain Text)
  1. .
  2. .
  3. .
  4. //Using negative subscripts
  5. cout<<"\nUsing negative subscripts"<<endl;
  6. p = a + n-1;
  7. for (size_t i =0; i < n ; ++i)
  8. cout << p[-i] << ' '; // <------ There is an extra space.
  9. cout<< endl;

Why does it display what it does, instead of just add two spaces between each number?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Wolf CCMLG is offline Offline
20 posts
since Sep 2009
Sep 20th, 2009
0

Re: Question about extra whitespace

..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
Reputation Points: 46
Solved Threads: 3
Light Poster
KonkaNok is offline Offline
38 posts
since May 2009
Sep 20th, 2009
0

Re: Question about extra whitespace

that is the problem
c++ Syntax (Toggle Plain Text)
  1. cout << 'a'; // good
  2. cout << ' '; // good
  3. cout << 'ab'; // bad
  4. cout << ' '; // bad
Last edited by NathanOliver; Sep 20th, 2009 at 9:56 pm.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
Sep 20th, 2009
1

Re: Question about extra whitespace

OK, you found the compile time error.

Now, there's still a potentially fatal logical error to be corrected.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Input a number and store it in an array
Next Thread in C++ Forum Timeline: is it a template related errors?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC