while loop segmentation fault

Apr 18, 2011 at 7:07pm
Hi all,

I have written a code, and in the beginning of it, it should get input from command line and put them in vectors. That part of my code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
while(cin>>name>>surname>>id>>by){ // takes the inputs

// pushes the values into the vectors

namevect.push_back(name);

surnamevect.push_back(surname);

idpvect.push_back(id); 

bypvect.push_back(by); 

}


But when I press Ctrl-D it gives a segmentation fault error. It has nothing to do with the vectors (I think), because I tried them all and printed out the contents and it was alright. Nothing happens when I'm in while loop, segmentation fault happens when I go out of the loop.
I could not find a solution. Any ideas?

Thanks in advance.
Last edited on Apr 18, 2011 at 7:10pm
Apr 18, 2011 at 7:25pm
Can we see all of the code around the loop?
Apr 18, 2011 at 7:34pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
string name, surname, id, by;

vector <string> namevect; // vector for names

vector <string> surnamevect; // vector for surnames

vector <string> idpvect; // vector for id's

vector <string> bypvect; // vector for birth years


while(cin>>name>>surname>>id>>by){ // takes the inputs

// pushes the values into the vectors

namevect.push_back(name);

surnamevect.push_back(surname);

idpvect.push_back(id); 

bypvect.push_back(by); 

// I also tried to print out the contents of the vectors here, it prints them all right.

}
// when I put a printing statement here, out of the loop, it does not get printed 


The problem is, as I said, when the loop terminates :/
Apr 18, 2011 at 7:49pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using namespace std;
#include <iostream>
#include <vector>
#include <string>

int main()
{

string name, surname, id, by;

vector <string> namevect;
vector <string> surnamevect; 
vector <string> idpvect; 
vector <string> bypvect; 


while(cin>>name>>surname>>id>>by){ 

namevect.push_back(name);
surnamevect.push_back(surname);
idpvect.push_back(id);
bypvect.push_back(by);


}
}
Last edited on Apr 18, 2011 at 7:49pm
Apr 18, 2011 at 7:51pm
yes, sorry, i forgot to add them here, i have them already in my code.
Apr 18, 2011 at 7:52pm
Is there still a problem or did a misread and did something different / didnt solve your problem.
Apr 18, 2011 at 7:53pm
still there is a problem...
Apr 18, 2011 at 7:55pm
I dont understand your problem clearly, can you rephrase or show a picture?
Apr 18, 2011 at 7:55pm
Hm, I see nothing wrong with that code...What compiler/IDE are you using?
Apr 18, 2011 at 7:58pm
The code is working great for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using namespace std;
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

void print (string elem)
{
    cout << elem << ' ';
}

int main()
{

string name, surname, id, by;
vector <string> namevect;
vector <string> surnamevect;
vector <string> idpvect;
vector <string> bypvect;


while(cin>>name>>surname>>id>>by)
{
    if (name == "done")
{
break;
}
namevect.push_back(name);
surnamevect.push_back(surname);
idpvect.push_back(id);
bypvect.push_back(by);

}

for_each (namevect.begin(), namevect.end(), print);
    cout << endl;
    for_each (surnamevect.begin(), surnamevect.end(), print);
    cout << endl;
    for_each (idpvect.begin(), idpvect.end(), print);
    cout << endl;
    for_each (bypvect.begin(), bypvect.end(), print);
    cout << endl;

}
Apr 18, 2011 at 8:02pm
@firedraco , g++
I give inputs to this program from the command line. It is supposed to push them in 4 seperate vectors, and does that, too. (I can print them in the loop). But when I press Ctrl-D in order to go out of the while loop, it gives segmentation fault error. And dos not even go through the statement after the loop. So there is something wrong there with the while loop, but could not see why.

p.s. I use g++ that comes with the centos linux distribution, on a virtual machine.
by the way, it compiles, but gives this error while running
Last edited on Apr 18, 2011 at 8:12pm
Apr 18, 2011 at 8:11pm
Hm. Try using ctrl+z instead and see if it still dies.
Apr 18, 2011 at 8:18pm
Since I use Linux, Ctrl-D is for the signal end-of-file, whereas Ctrl-Z is the "stop" shortcut. So it just says "stopped" when I press it.
Apr 18, 2011 at 8:19pm
Do in your loop

!cin.eof()
Apr 18, 2011 at 8:21pm
the same... :(
Apr 18, 2011 at 8:23pm
cin.eof() or !cin.eof() is the only thing i can think of which will see for end of file. Sorry.
Apr 18, 2011 at 8:25pm
but, you think my original code is alright, don't you? it is supposed to be. but, well... i just got too sick and tired of all these stuff! :S
Apr 18, 2011 at 8:58pm
Hrm...honestly, I have no idea...try debugging it and see what happens.
Topic archived. No new replies allowed.