943,542 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 455
  • C++ RSS
Dec 14th, 2008
0

string question

Expand Post »
This's got probably an easy answer but it's been puzzling me a few hours already.
The code below declares a string object and fills it character by character. I can print every single character (line 16) but I can't print the string as a whole (line 19).
Could anybody tell me why is that so and how to fix it, please?

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. string a;
  8.  
  9. cout << "String a is: " << a << endl; //Nothing expected, fine
  10. char tmp= 100;
  11. for (int i=0; i<10; i++)
  12. {
  13. a[i]= tmp;
  14. tmp++;
  15. }
  16. for (int i=0; i<10; i++)
  17. cout << a[i];
  18.  
  19. cout << "\na: " << a << endl;
  20. return 0;
  21. }
Similar Threads
Reputation Points: 34
Solved Threads: 2
Junior Poster in Training
freelancelote is offline Offline
88 posts
since Aug 2008
Dec 14th, 2008
1

Re: string question

Change a[i]= tmp; with a += tmp; .
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 14th, 2008
2

Re: string question

>string a;
This is an empty string.

>a[i]= tmp;
a[i] doesn't exist. a is still an empty string. The subscript operator does not change the size of a collection.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: Help with Student Grades program
Next Thread in C++ Forum Timeline: Malfunctioning LOOP





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


Follow us on Twitter


© 2011 DaniWeb® LLC