Thread: Small program crash with C++ Bloodshev

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    Small program crash with C++ Bloodshev

    this is my code
    Code:
    #include <stdio.h>//standard input and output
    #include <stdlib.h>//standard library
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
     char *database[100][100]={{"Z","L","A","N","K"},{"H","B","A"},{"B","H"},{"B","A"},{"H","G"},{"H","G","F"},{"H","G","F","K"},{"H","G","F","K","L"},
    
    {"H","G","L"},{"H","G","L","F"},{"H","G","M"},{"H","G","M","Q"},{"H","G","M","Q","P"},{"B","H","G"},{"B","H","G","F"},{"B","H","G","F","K"},
    
    {"B","H","G","F","K","L"},{"B","H","G","L"},{"B","H","G","L","F"},{"B","H","G","M"},{"B","H","G","M","Q"},{"B","H","G","M","Q","P"},{"G","F"},
    
    {"G","F","K"},{"G","F","K","L"},{"G","L"},{"G","L","F"},{"G","M"},{"G","M","Q"},{"G","M","Q","P"},{"F","K","L"},{"F","K"},{"L","F"},{"K","L"},
    
    {"K","L","F"},{"M","Q","P"},{"Q","P"},{"H","D"},{"H","D","C"},{"H","D","I"},{"H","D","I","J"},{"H","D","I","J","E"},{"H","D","I","J","O"},
    
    {"H","D","I","J","O","N"},{"D","C"},{"D","I"},{"D","I","J"},{"D","I","J","E"},{"D","I","J","O"},{"D","I","J","O","N"},{"I","J"},{"I","J","E"},{"I","J","O"},
    
    {"I","J","O","N"},{"J","E"},{"E","J"},{"J","O","N"},{"O","N"},{"O","N","J"},{"O","I","J"},{"O","I","J","E"},{"D","C"},{"D","I","C"},{"O","I","C"},
    
    {"J","O","I","C"},{"E","J","O","I","C"},{"N","J","I","C"},{"J","I","C"},{"E","J","I","C"},{"R","Q","P"},{"R","Q","M"},{"R","S","M"},{"R","S","T"},{"S","T"},
    
    {"Q","M"},{"Q","P"},{"S","M"},{"S","T"}};
     char initial,pointer[100],final[0][0],results,pointer2[100][100];
     char database2[100][100]={{"Blank"},{"HBA"},{"BH"},{"BA"},{"HG"},{"HGF"},{"HGFK"},{"HGFKL"},{"HGL"},{"HGLF"},{"HGM"},{"HGMQ"},
    
    {"HGMQP"},{"BHG"},{"BHGF"},{"BHGFK"},{"BHGFKL"},{"BHGL"},{"BHGLF"},{"BHGM"},{"BHGMQ"},{"BHGMQP"},{"GF"},{"GFK"},{"GFKL"},{"GL"},
    
    {"GLF"},{"GM"},{"GMQ"},{"GMQP"},{"FKL"},{"FK"},{"LF"},{"KL"},{"KLF"},{"MQP"},{"QP"},{"HD"},{"HDC"},{"HDI"},{"HDIJ"},{"HDIJE"},{"HDIJO"},
    
    {"HDIJON"},{"DC"},{"DI"},{"DIJ"},{"DIJE"},{"DIJO"},{"DIJON"},{"IJ"},{"IJE"},{"IJO"},{"IJON"},{"JE"},{"EJ"},{"JON"},{"ON"},{"ONJ"},{"OIJ"},
    
    {"OIJE"},{"DC"},{"DIC"},{"OIC"},{"JOIC"},{"EJOIC"},{"NJIC"},{"JIC"},{"EJIC"},{"RQP"},{"RQM"},{"RSM"},{"RST"},{"ST"},{"QM"},{"QP"},{"SM"},
    
    {"ST"}};
     int i,j,test;
    
     
     
     int main()
     {
    puts("Enter the starting point");
    gets(&initial);
    puts("Enter the ending point");
    gets(&final[0][0]);
    
    {
    for(i=0;i<100;i++)
    {
    strcpy(&pointer[i],database[i][0]);
    
    
    if (strcmp(&initial,&pointer[i])==0)
    
    
    for (j=0;j<100;j++)
    {
    strcpy(&pointer2[i][j],database[i][j]);
    
    if ((strcmp(&final[0][0],&pointer2[i][j])==0))
    
    
    puts(&database2[i][0]);
    
    
    }
    }
    }
    
    
       system("pause");
        return(0);
    }

    It crashes after it had found a letter in the database that matches the first letter of the initial input. The purpose is just to find a string in the database that matches the given starting letter and ending letter.

    I'm not sure why it crashes... sorry if this is asking a lot. oh and i moved the database a little to not stretch the post

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh... my... goodness! Horrible code.
    1) Indent.
    2) Never use gets: http://cpwiki.sourceforge.net/Gets
    3) Why do you think it crashes when you pass the value of final[0][0] which is 0? You are passing a value, not an address. Enable warnings!
    4) How do you expect it to be able to read something when there's no space to store it in?

    And this is pure and utter C, not C++. Thank you very much.
    These are just a small portion of what's wrong.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Before you populate it with 100, try it with a small number, like 5.

    > final[0][0]
    How many chars does this hold?
    0
    1
    2
    as many as you like
    ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    oh. Thank you for the help. its working better.
    my bad on the wrong section thing. I just looked at the top right and it said Dev-C++ :S

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my program crash?
    By vrkiller in forum C++ Programming
    Replies: 10
    Last Post: 03-04-2009, 03:03 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Problem using structures (they crash my program)
    By shadow1515 in forum C Programming
    Replies: 7
    Last Post: 06-17-2008, 08:36 AM
  4. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 10:52 AM
  5. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 12:42 PM