Dynamic array problem

This is a discussion on Dynamic array problem within the C++ Programming forums, part of the General Programming Boards category; The following program crashes. Can someone please tell me what's wrong in this code snippet? Code: int i, n = ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Dynamic array problem

    The following program crashes. Can someone please tell me what's wrong in this code snippet?

    Code:
    int i, n = 0, num;
        string data;
    
        string *cargo_name_array;
        int *cargo_duration_array;
        int *cargo_size_array;
    
    ifstream cargo_in("cargo_input.txt"); //opens input file
    
            //gets number of lines
            if (cargo_in.is_open()) {
                while (cargo_in.good()) {
                    getline(cargo_in,data);
                    n++; //line counter up
                };
            }
    
            else { //file access verification
                cout << "Input file access error.";
                return 0;
            };
    
            cargo_in.clear();
    
            //declares dynamic arrays for cargo parameters
            cargo_name_array = new string[n];
            cargo_duration_array = new int[n];
            cargo_size_array = new int[n];
    
            for (i=1; i<=n; i++) {
                getline(cargo_in, data, '\t'); //gets string until tab
                cargo_name_array[i] = data; //stores string in array
    
                while (cargo_in.get() == '\t'); //discards consecutive tabs
                cargo_in.unget(); //moves back one character
    
                getline(cargo_in, data, '\t'); //gets size string until tab
                //converts string to decimal and stores size value
                cargo_size_array[i] = (int) strtod(data.c_str(), NULL);
    
                getline(cargo_in, data); //gets duration string
                //converts string to decimal and stores duration value
                cargo_duration_array[i] = (int) strtod(data.c_str(), NULL);
            }
                cargo_in.close(); //closes input file

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    A possible issue that your array only has n elements, not n + 1. But you iterate from 0 to n (that is, a total of n + 1 elements).
    Last edited by Elysia; 05-09-2011 at 11:16 AM. Reason: Spelling error
    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.

  3. #3
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    You're right, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamic Array
    By delphi in forum C Programming
    Replies: 5
    Last Post: 05-17-2007, 08:39 PM
  2. Dynamic array problem
    By new here in forum C++ Programming
    Replies: 9
    Last Post: 03-12-2003, 05:39 PM
  3. Dynamic Array Problem
    By adc85 in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 01:29 PM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 09:02 PM
  5. Dynamic array problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2002, 09:55 AM

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