Question about extra whitespace

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 15
Reputation: Wolf CCMLG is an unknown quantity at this point 
Solved Threads: 0
Wolf CCMLG Wolf CCMLG is offline Offline
Newbie Poster

Question about extra whitespace

 
0
  #1
Sep 20th, 2009
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:
  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:
  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?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 36
Reputation: KonkaNok is on a distinguished road 
Solved Threads: 3
KonkaNok's Avatar
KonkaNok KonkaNok is offline Offline
Light Poster

Re: Question about extra whitespace

 
0
  #2
Sep 20th, 2009
..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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Question about extra whitespace

 
0
  #3
Sep 20th, 2009
that is the problem
  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.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Question about extra whitespace

 
1
  #4
Sep 20th, 2009
OK, you found the compile time error.

Now, there's still a potentially fatal logical error to be corrected.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 256 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC