C++ vectors problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 10
Reputation: mugun has a little shameless behaviour in the past 
Solved Threads: 0
mugun mugun is offline Offline
Newbie Poster

C++ vectors problem

 
-1
  #1
Oct 5th, 2008
hello everyone....i need some help,i am using c++ and vectors, and text files to create a library system and i have a problem.....i used a vector named v1 and i was trying to use an if else to assign a created vectors elements value to another value

example like v[10] is "Storybook5"
i tried this v[10]="storybook3"
and this also v.at(10)="storybook3"

it still will not change,it ll come out Storybook5

i need help.....this is my code.....
  1. void LibraryMember::PrintLoanMem()//this is to print the members on loan,and reduce repetition of members
  2. {
  3. v1.clear();
  4. ifstream loanfile("loan.txt");
  5. string info;
  6. int i;
  7. int j;
  8. int count=0;
  9. int count2=0;
  10. if (loanfile.is_open())
  11. {
  12. while(!loanfile.eof())
  13. {
  14. getline(loanfile,info);
  15. v1.push_back(info);
  16. count++;
  17. }
  18. }
  19. count2=count;
  20. v1[3]="";
  21. for(i=0;i<count;i++)
  22. {
  23. for(j=0;j<count2;j++)
  24. {
  25. if (v1[j]==v1[i])
  26. {
  27. v1[j]=="";
  28. }
  29. }
  30. }
  31. ofstream out("loanMem.txt",ios::app);
  32. size_t size = v1.size();
  33. for (size_t i = 0; i < size; i++)
  34. {
  35. if(v1.at(i)=="")
  36. {
  37. }
  38. else
  39. out << v1[i] <<endl;
  40. }
  41. }//end function printloan mem
Last edited by Ancient Dragon; Oct 5th, 2008 at 6:42 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: C++ vectors problem

 
0
  #2
Oct 5th, 2008
I'd appreciate some more code
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: C++ vectors problem

 
0
  #3
Oct 5th, 2008
More specifically, a small piece of code that actually exhibits the behaviour you consider to be a problem.

The code you've given does not appear related to the text in your post in any way.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: C++ vectors problem

 
0
  #4
Oct 5th, 2008
Indeed, however i have a feeling it may be a similar problem to this....

  1. if (v1[j]==v1[i])
  2. {
  3. v1[j]=="";
  4. }

What exactly does this do?
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: C++ vectors problem

 
0
  #5
Oct 5th, 2008
I think it's a problem with your understanding of the std::vector class properties.
Before using v[10] you must initialize v[10] vector element. For example, you can declare std::vector<type> v(N) or call v.push_back(something) at least N times or call v.resize(N) where N >= 11.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC