Thread: Weird crash error I can't figure out

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    Weird crash error I can't figure out

    My problem is that it runs though strcpy(*Ingrediants, temp) the first time then run times out the second time though.

    This code is meant to populate a dynamic array using all the words from a .txt I made myself.

    Does anyone know why this is happening?

    Code:
    int main()
    {
    	char ** Ingrediants = 0;
    	int * counter = 0;
    	ifstream myfile("pantry.txt");
    	int i = 0;
    	int numberOfIngrediants = 0;
    	int choice = 0;
    	char goAgain = 'n';
    
    	while(goAgain = 'y'){
    	//Checks to see if file opens.
    	if(myfile.fail())
    	{
    		cout << "File did not open";
    	}
    
    	//Populates ** Ingrediants from "pantry.txt"
    	while(!myfile.eof())
    	{
    		char temp[30];
    		myfile.getline(temp, 30, '\n');
    		char ** Ingrediants = new char*[numberOfIngrediants];
    		Ingrediants[numberOfIngrediants] = new char (strlen(temp));
    		strcpy(*Ingrediants, temp);
    
    
    		//char ** NewIngrediants = new char*[numberOfIngrediants + 1];
    		//NewIngrediants = Ingrediants;
    		i++;
    		numberOfIngrediants++;
    //		=======================

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jesse20ghet
    Code:
    char ** Ingrediants = 0;
    // ...
    char ** Ingrediants = new char*[numberOfIngrediants];
    Ingrediants[numberOfIngrediants] = new char (strlen(temp));
    strcpy(*Ingrediants, temp);
    This looks very wrong, because you:
    • Created a variable named Ingrediants that is local to the loop and which hides the variable named Ingrediants that is local to the function.
    • Allocated numberOfIngrediants pointers, then access the pointer at the numberOfIngrediants index, which does not exist.
    • Allocated space for a single char with value strlen(temp) when you probably wanted to allocate space for strlen(temp) + 1 chars.

    Instead of all this mess, use a std::vector<std::string> instead.

    Oh, and while we are at it, this is wrong:
    Code:
    while(goAgain = 'y'){
    as you are assigning to goAgain when you probably intended to compare. Also, this is wrong:
    Code:
    while(!myfile.eof())
    Rather, use the return value of getline to control the loop.
    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

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    I fixed most of what you said but I'm only familiar with myfile.eof so that's why I use it, I can't use strings as this is a school assignment and its driving me crazy. I still get the same error though? Where on the 2nd pass through, strcpy(*NewIngrediants, temp) gets a bad address.

    Code:
    int main()
    {
    	char ** Ingrediants = 0;
    	int * counter = 0;
    	ifstream myfile("pantry.txt");
    	int i = 0;
    	int numberOfIngrediants = 0;
    	int choice = 0;
    	char goAgain = 'n';
    
    	while(goAgain == 'y'){
    	//Checks to see if file opens.
    	if(myfile.fail())
    	{
    		cout << "File did not open";
    	}
    
    	//Populates ** Ingrediants from "pantry.txt"
    	while(!myfile.eof())
    	{
    		char temp[30];
    		myfile.getline(temp, 30, '\n');
    		char ** NewIngrediants = new char*[numberOfIngrediants];
    		NewIngrediants[numberOfIngrediants] = new char (strlen(temp) + 1);
    		strcpy(*NewIngrediants, temp);
    
    		Ingrediants[numberOfIngrediants] = *NewIngrediants;
    		delete [] NewIngrediants;
    		i++;
    		numberOfIngrediants++;

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    char ** NewIngrediants = new char*[numberOfIngrediants];
    NewIngrediants[numberOfIngrediants] = new char (strlen(temp) + 1);
    You create an array of character pointers which is numOfIngrediants long. The valid indexes of that array are 0 to numberOfIngrediants - 1. Now, what do you suppose happens when you access NewIngrediants[numberOfIngrediants] when the last valid index in the array is actually numberOfIngrediants - 1?

    It's so hard to intentionally keep someone's spelling mistakes in the code when trying to help. It's Ingredients, not Ingrediants

    EDIT: Oh, wait a minute. I assumed you had a sane value of numberOfIngrediants, but you don't...you're allocating a 0-length array. Shouldn't do that. Do you know the number ahead of time? If not, try reading through to count the number of lines, then rewind or close and re-open the file, allocating the correct number of elements.
    Last edited by rags_to_riches; 10-11-2011 at 05:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. crash error
    By morrissey999 in forum C Programming
    Replies: 6
    Last Post: 04-30-2011, 01:55 PM
  2. cant figure out error
    By rozwald in forum C++ Programming
    Replies: 2
    Last Post: 07-08-2010, 02:20 AM
  3. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  4. Can'nt figure out ERROR
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 01-27-2006, 11:07 AM
  5. I cant figure out how to fix this error
    By Raigne in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2005, 02:04 AM