Segmentation Fault

I am familiar with Windows programming, but totally new to *nix so I hope this isn't a stupid question... This program causes a segfault right where I put SEGFAULT. It prints out the first two cout's, doesn't give me a chance to enter any input, and then segfaults.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <iostream>
#include <gmp.h>

using namespace std;

int main() {
    char i[500], j[500], *charp;
    bool acceptable = true;
    
    cout << "Blah blah blah?\n";
    cin >> i;
    cout << "Blah blah blah?\n";
    cin >> j;
    SEGFAULT
    cout << "done";

    program continues on down here...

What is going on here?
I see no reason for that program to behave as you describe. Is there anything between lines 7 and 12 you omitted?
My code exactly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>

using namespace std;

int main()
{
    mpz_t start, finish, p, root, factoringnum, null, mpz_i, mpz_j;

    char j[500], i[500], str[500];
    bool prime = true;

    cout << "Where to start? ";
    cin >> i;
    cout << "\nWhere to finish? ";
    cin >> j;
    SEGFAULT
    cout << "done";
    
    program continues...

I don't know what was going on earlier, but now, everything works fine up until the segfault. It lets me input the two numbers and then crashes without printing done. Thanks for your help!
It seems a bit odd that you are reading in user values as the addresses for the arrays, but I don't think that alone would cause a segmentation fault. Simply setting the pointer to invalid memory shouldn't cause a problem like that.
Are there any *nix specific methods of input I could try? Since cin doesn't seem to be working so well, and gets() is "unsafe" apparently.
Try using cin.getline().
Topic archived. No new replies allowed.