Thread: Segmentation Fault (core dumped)

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Post Segmentation Fault (core dumped)

    I'm working on a decoding program, and I'm having trouble with a string which I want to give a variable size, which causes me "Segmentation Fault (core dumped)" if I give it a constant size and then "str.resize" it, debugger tells me:
    error: request for member ‘resize’ in ‘line’, which is of non-class type. The problem is with line 74.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    string code = "the quick brown fox jumps over the lazy dog";
    int x = code.length();
    
    int checkCode(string a[], int size) {
        int numcode = 8;
        int numspace = 0;
        int numret = 0;
        for (int i = 0; i < size; i++) {
            string b = a[i];
            int c = b.length();
            if (c == x) {
                for (int j = 0; j < c; j++) {
                    if (b[j] == ' ') numspace++;
                }
                if (numspace == numcode) {
                    numret = i;
                    i = size;
                }
            }
        }
        return numret;
    }
    
    string getCode(string d) {
        string dec = "abcdefghijklmnopqrstuvwxyz";
        char loc = ' ';
        int q = d.length();
        for (int i = 0; i < q; i++) {
            if (d[i] != ' ') {
                loc = d[i] - 'a';
                dec[loc] = code[i];
            }
        }
        return dec;
    }
    
    void decode(string s[], int size, string d, string name) {
        char loc = ' ';
        ofstream arch;
        arch.open(name.c_str());
        for (int i = 0; i < size; i++) {
            string n = s[i];
            int t = n.length();
            for (int j = 0; j < t; j++) {
                if (n[j] != ' ') {
                    loc = n[j] - 'a';
                    n[j] = d[loc];
                }
            }
            arch << n << endl;
        }
    }
    
    int main() {
        int num = 0,codeN,cont;
        string dec, name;
    
        cout << "Coded file name: ";
        cin >> name;
    
        //Open file and creates an array based on size
        ifstream arch;
        arch.open((name).c_str());
        while (getline(arch,dec)) {
            num++;
        }
        string line[30];
        line.resize(num);
    
        arch.clear();
        arch.seekg(0);
        cont = 0;
        while (!arch.eof()) {
            getline(arch,line[cont]);
            cont++;
        }
        arch.close();
    
        cout << "Decoded file name: ";
        cin >> name;
    
        codeN = checkCode(line,num);
        dec = getCode(line[codeN]);
        decode(line,num,dec,name);
    
        cout << "File ready: " << name << endl;
    
        return 0;
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The problem is with line 74.
    O_o

    No. The problem is with you.

    An array of thing is not an instance of thing; it is an array of thing.

    Consider using a `std::vector<std::string>'; I don't see you doing this right without it.

    Soma

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And while you're at it, use push_back instead of resize. Read into a temp string, then push back.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Thank you for all the commentary. As a beginner it's helpful to get feedback on conventions to make my code better. I'm bumping myself in the head for trying to use an array in that way.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Is there such a function in C/C++ for resizing of arrays called 'resize'? Google doesn't help me find it, I want to guess it doesn't exist.
    Check out my programming / algorithm blog @ http://www.swageroo.com

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is no such function, hence phantomotap's suggestion of using a std::vector instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (core dumped)
    By sameer2904 in forum C Programming
    Replies: 3
    Last Post: 01-09-2012, 07:37 AM
  2. Segmentation fault (core dumped)
    By jonagondo in forum C Programming
    Replies: 6
    Last Post: 01-04-2012, 03:56 PM
  3. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  4. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  5. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM