Jump to content





Recent Status Updates

  • Photo
      16 Apr
    Kadence

    If you're reading this, you're on my profile and I know you're on my profile because I'm probably viewing yours.

    Show comments (6)
  • Photo
      10 Apr
    Poe

    Finally (and hopefully) i'm getting a team together that knows a little of this and a little of that; and maybe all my open source projects that are half written can begin to be released. :)

View All Updates
Photo
- - - - -

Why the hell does my program keeps crashing the console?

console

  • Please log in to reply
11 replies to this topic

#1 hayloiuy

hayloiuy

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 17 April 2010 - 09:59 AM

Ok i have an assignment that required me to create a simple database using C++. I tried using both ways which is structures and 2d arrays. But it just keep crashing the command prompt!! Can somebody take a look at the code and tell me the problem? Thank you very much in advance. P.S: I am new to programming. I can hardly understand classes so please be detailed in your explaination.
---------------------------------------------------------------
the structure program
------------------------------------------------------------
#include <iostream>

using namespace std;


int add_record()
{struct weapon
{int id;
int name;
int quantity;
};

    int i,k,size,l; 
i=1;
l=0;
k=1;
size=0;
 weapon add[size];
cout<< "Enter size of record"<< endl;
cin>> size;


    for( l=0; l<size;l++)
    {
                  cin>> add[l].id; 
                  cin>> add[l].name;
                   cin>> add[l].quantity;
    }
    

    
for( l=0; l<size;l++)
{ 
     cout<< add[l].id<< endl;
     cout<< add[l].name<< endl;
     cout<< add[l].quantity<< endl;
}

     return 0;
}
int main()
{
    add_record();
    
    system ("PAUSE");
    return 0;
}
=========================================================
the array prog
=========================================
#include <iostream>

using namespace std;

int  main()
{int row, a,b;
b=0;
const int column=3;
     char weapon [row][column];
     row=1;
     cout<< "Please enter row "<< endl;
     cin>> row ;
     
     for ( a=0; a<row; a++)
     {b=0;
          for (b=0; b<3; b++)
          { cout << "Enter id, quantity,name" <<endl;
            cin>>weapon[a][b];
            }
            };
            
            for ( a=0; a<row; a++)
     {b=0;
          for (b=0; b<3; b++)
          { 
            cout<< weapon[a][b]<< " ";
            }
            
            cout<< endl;
            };
            system ("pause");
            return 0;
            }
     
_--------------------------------------------------------------------------------

Sorry i post the wrong structure program now i updated the structure program

Edited by WingedPanther, 17 April 2010 - 11:00 AM.
add code tags

  • 0

#2 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 16,631 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 17 April 2010 - 11:10 AM

You're creating arrays of 0 elements. Why? You need to use malloc and pointers if you want variable sized arrays.
  • 0
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3 hayloiuy

hayloiuy

    CC Newcomer

  • Just Joined
  • PipPip
  • 18 posts

Posted 17 April 2010 - 08:16 PM

See thats my problem . i am new to programming. Mallots ? Pointers?
  • 0

#4 veda87

veda87

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 17 April 2010 - 08:40 PM

So, you need to know about Dynamic memory allocation. Then go through this tutorial
http://forum.codecal...oc-realloc.html
  • 0

#5 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

Posted 17 April 2010 - 08:52 PM

You are obviously way over your head -- you have not yet learned about malloc() and pointers, yet you post a program that attempts to use them?

Here is a good tutorial on pointers.
  • 0
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#6 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 16,631 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 18 April 2010 - 04:14 AM

Since this is C++, you would normally use new instead of malloc. That said, you CANNOT create an array of size 0 and expect to accomplish much with it.
  • 0
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

Posted 18 April 2010 - 05:47 AM

Yes you can create arrays of size 0. int* array = new int[0]; is a perfectly valid statement which will compile and execute without fail. In this case new will return a valid pointer to a memory block of 0 length. What can you do with it? Not much, I admit.
  • 0
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#8 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1,726 posts

Posted 18 April 2010 - 02:05 PM

Yes you can create arrays of size 0. int* array = new int[0]; is a perfectly valid statement which will compile and execute without fail. In this case new will return a valid pointer to a memory block of 0 length. What can you do with it? Not much, I admit.


Why did you even say this? You tried to disagree with WingedPanther then just confirmed what he said. He simply says that if you do create an array of size 0 you shouldn't expect to do much with it. Which is exactly what you wrote.

MorePostsForMyEPenisPlease.
  • 0

#9 dargueta

dargueta

    CC Mentor

  • Moderator
  • 4,386 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 18 April 2010 - 02:52 PM

@MeTh0Dz: I think Ancient Dragon just misinterpreted what WingedPanther said.

That being said, I believe there's another problem with hayloluy's code. Aside from the fact that the weapons array is being declared with a row size of zero, it appears that weapon IDs, quantities and names can only be one character long total:

char weapon[row][column];
... blah ...
cout << "Enter ID, quantity, name." << endl;
cin >> weapon[a][b];

I highly doubt this is the case, so you're going to need to fix this. Either set a maximum limit on the length of the id/quantity/name string, or use an std::string class.
/* either this */
const int max_length = 32;
char weapon[row][column][max_length];

/* or this */
std::string weapon[row][column];
Given the level of your coding ability, I suggest you go with the former.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#10 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

Posted 18 April 2010 - 08:58 PM

After re-reading post #6 I agree you are right -- I did mis-interpret what he said. When I first read it I thought (wrongly) that he said you can not create arrays of size 0. But now I understand that wasn't what he said at all. So just ignore my stupid blunder.:D
  • 0
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#11 Firebird_38

Firebird_38

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

Posted 19 April 2010 - 01:41 AM

Suggestion: Have someone do the work for you. You're not versed enough to do this.

Alternatively, select a language that makes it easier to do things like dynamic arrays and dynamic strings, like Delphi or so.

As was already stated: you're in way over your head. But you can do this, just consider ahead of time that even this simple example will take you weeks to figure out. That is because your trying to "speak" a language that you really don't know. And with programming, you really need to understand intricacies.
  • 0

#12 dargueta

dargueta

    CC Mentor

  • Moderator
  • 4,386 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 19 April 2010 - 11:30 AM

If he has the right kind of help (i.e. us) then I don't think it'll take weeks. A while, yes.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: console