Segmentation Fault (core dumped)

This is a discussion on Segmentation Fault (core dumped) within the C++ Programming forums, part of the General Programming Boards category; I'm working on a decoding program, and I'm having trouble with a string which I want to give a variable ...

  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
    4,415
    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
    Posts
    23,021
    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
    22,303
    There is no such function, hence phantomotap's suggestion of using a std::vector instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08:50 AM
  4. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 02:18 PM
  5. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21