Reading certain values of a vector and write them into another vector

Greetings,

I couldn't succeed on doing that. Here's basic parts of my code that i tried to read certain rows within a vector and tried to write them into another vector..

1
2
3
4
5
6
7
8
9
10
11
 int c=0;
    cout << '\n';
    cout << "QLT MLC Values:" << '\n';
	for (int i=0; i<Numbers.size(); i++){
    if (i>=74 && i<=104){
    QLT[c]=Numbers[i];
    cout << "Carriage-B QLT MLC Number " << i-59 << " = " << QLT[c] << '\n'; 
    i=i+1;
    c=c+1;
    }
	}


But when i run this code, i've got "Segmentation Fault" all the time. Why do i get it? I couldn't understand that..

By the way, this loop of logical state skips every first element that comes after reading rows which i want to store. So it reads rows like "74, 76, 78, ... 104".

Any help would be great = /

Thank you.
Last edited on
Does QLT.size() == 0 at line 1? Vectors don't grow automatically by writing outside their bounds, you have to grow them explicitly by calling resize(), push_back(), or emplace_back(). You should change lines 6 and 7 to
1
2
QLT.push_back(Numbers[i]);
cout << "Carriage-B QLT MLC Number " << i-59 << " = " << QLT.back() << '\n';


By the way, check your editor settings. It's mixing tabs and spaces, which is frowned upon by most people. For example, it makes your formatting unreadable on this site.
Hello,

Does QLT.size() == 0 at line 1?


Yes.

I tried "Push back" but Values couldn't be written on the screen. After that i just tried what i do in matlab all the time. I thought it could work but it did not.. I didn't know ".back()" part of code. Thank you very much for helping me. Its working now.

Apologize but I dont understand? What do you mean "mixing tabs and spaces" ? I use Code::Blocks as an Editor in Linux. I did look settings but couldn't see anything related to this
Topic archived. No new replies allowed.