Like Tree1Likes
  • 1 Post By laserlight

Weird crash error I can't figure out

This is a discussion on Weird crash error I can't figure out within the C++ Programming forums, part of the General Programming Boards category; My problem is that it runs though strcpy(*Ingrediants, temp) the first time then run times out the second time though. ...

  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
    21,440
    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.
    Salem likes this.
    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

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    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, 10: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, 01:04 AM

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