Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Unhandled exception...

Unhandled exception...

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SilverShalkin
    wrote on last edited by
    #1

    ok... I've had problems with unhandled exception error before... but right when i fixed it, i kind of forgot what i did to fix it... this is my code... "im posting it all.. except my header file, just to make sure you know what im doing." #include #include "Class.h"//included header "Class.h" using namespace std; Name person; //class def person int NN = 1;//incriment char Names[21]; //total amount of names char SName[100]; //SetName function. bool Name::SetName(char* NName) { if(!NName) { return false; } int NLen = (strlen(NName) + 1); char *NewName = new char[NLen]; if(!NewName) { return false; } strcpy(NewName, NName); delete[] M_Name; M_Name = NewName; Names[NN] = *M_Name;//I think it has somthing to do with this NN++; return true; } const char* Name::GetName() { return M_Name; } //main int main() { for(;;)//main loop { gets(SName); person.SetName(SName); cout << person.GetName() << endl << endl; //print name that you just typed. for(int inc;inc < NN;inc++) //print all names. { cout << Names[inc] << endl; //error unhandled exception. }//end of print all names, loop. inc = 1; }//end main loop. return 0; } //end main //constructor & deconstructor //******************* Name::Name() { M_Name = NULL; } Name::~Name() { delete[] M_Name; } The error accured when i tried to set Names[NN] = *M_Name; the error feels like its at the tip of my tongue, yet i cant think of it. Thanks all for the help! ~SilverShalkin ps... how do you show a screen shot of your code? or post your code on CP with its background different... "is ther a place on the forum that shows how?" Thanks! :rose:

    S C R 3 Replies Last reply
    0
    • S SilverShalkin

      ok... I've had problems with unhandled exception error before... but right when i fixed it, i kind of forgot what i did to fix it... this is my code... "im posting it all.. except my header file, just to make sure you know what im doing." #include #include "Class.h"//included header "Class.h" using namespace std; Name person; //class def person int NN = 1;//incriment char Names[21]; //total amount of names char SName[100]; //SetName function. bool Name::SetName(char* NName) { if(!NName) { return false; } int NLen = (strlen(NName) + 1); char *NewName = new char[NLen]; if(!NewName) { return false; } strcpy(NewName, NName); delete[] M_Name; M_Name = NewName; Names[NN] = *M_Name;//I think it has somthing to do with this NN++; return true; } const char* Name::GetName() { return M_Name; } //main int main() { for(;;)//main loop { gets(SName); person.SetName(SName); cout << person.GetName() << endl << endl; //print name that you just typed. for(int inc;inc < NN;inc++) //print all names. { cout << Names[inc] << endl; //error unhandled exception. }//end of print all names, loop. inc = 1; }//end main loop. return 0; } //end main //constructor & deconstructor //******************* Name::Name() { M_Name = NULL; } Name::~Name() { delete[] M_Name; } The error accured when i tried to set Names[NN] = *M_Name; the error feels like its at the tip of my tongue, yet i cant think of it. Thanks all for the help! ~SilverShalkin ps... how do you show a screen shot of your code? or post your code on CP with its background different... "is ther a place on the forum that shows how?" Thanks! :rose:

      S Offline
      S Offline
      soptest
      wrote on last edited by
      #2

      for(int inc;inc < NN;inc++) must initialize inc = 0; soptest

      C 1 Reply Last reply
      0
      • S SilverShalkin

        ok... I've had problems with unhandled exception error before... but right when i fixed it, i kind of forgot what i did to fix it... this is my code... "im posting it all.. except my header file, just to make sure you know what im doing." #include #include "Class.h"//included header "Class.h" using namespace std; Name person; //class def person int NN = 1;//incriment char Names[21]; //total amount of names char SName[100]; //SetName function. bool Name::SetName(char* NName) { if(!NName) { return false; } int NLen = (strlen(NName) + 1); char *NewName = new char[NLen]; if(!NewName) { return false; } strcpy(NewName, NName); delete[] M_Name; M_Name = NewName; Names[NN] = *M_Name;//I think it has somthing to do with this NN++; return true; } const char* Name::GetName() { return M_Name; } //main int main() { for(;;)//main loop { gets(SName); person.SetName(SName); cout << person.GetName() << endl << endl; //print name that you just typed. for(int inc;inc < NN;inc++) //print all names. { cout << Names[inc] << endl; //error unhandled exception. }//end of print all names, loop. inc = 1; }//end main loop. return 0; } //end main //constructor & deconstructor //******************* Name::Name() { M_Name = NULL; } Name::~Name() { delete[] M_Name; } The error accured when i tried to set Names[NN] = *M_Name; the error feels like its at the tip of my tongue, yet i cant think of it. Thanks all for the help! ~SilverShalkin ps... how do you show a screen shot of your code? or post your code on CP with its background different... "is ther a place on the forum that shows how?" Thanks! :rose:

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        M_Name is probably NULL and you're trying to deference it. You can make your code look different by wrapping it in a <pre> tag, but of course then you need to type & lt and & gt ( without the space ) to get your < > characters. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

        S 1 Reply Last reply
        0
        • S soptest

          for(int inc;inc < NN;inc++) must initialize inc = 0; soptest

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          I didn't even look through the code, but that is truly awesome. SilverShakin - step into the debugger sometime to see what amazingly huge negative numbers an unassigned int gets as a default value. One thing I like about C# - it sets sensible default values for variables. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

          1 Reply Last reply
          0
          • C Christian Graus

            M_Name is probably NULL and you're trying to deference it. You can make your code look different by wrapping it in a <pre> tag, but of course then you need to type & lt and & gt ( without the space ) to get your < > characters. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

            S Offline
            S Offline
            SilverShalkin
            wrote on last edited by
            #5

            int the for loop the for(int inc; the int initializes, or defines inc as an intiger... therefore i do not need to define it anywhere else... but ill look over it and see if i made a mistake... Thanks all! M_Name is = NULL,.... hmm im thinking... ill go and change some things... if i cant figuere it out, ill jump back on Thanks again! ~SilverShalkin :rose:

            C S 2 Replies Last reply
            0
            • S SilverShalkin

              int the for loop the for(int inc; the int initializes, or defines inc as an intiger... therefore i do not need to define it anywhere else... but ill look over it and see if i made a mistake... Thanks all! M_Name is = NULL,.... hmm im thinking... ill go and change some things... if i cant figuere it out, ill jump back on Thanks again! ~SilverShalkin :rose:

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              The problem is exactly as has been stated - you define an integer, but do not give it a value. It's value will be in the order of -80,0000. You need to put int inc = 0, not just int inc. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

              1 Reply Last reply
              0
              • S SilverShalkin

                int the for loop the for(int inc; the int initializes, or defines inc as an intiger... therefore i do not need to define it anywhere else... but ill look over it and see if i made a mistake... Thanks all! M_Name is = NULL,.... hmm im thinking... ill go and change some things... if i cant figuere it out, ill jump back on Thanks again! ~SilverShalkin :rose:

                S Offline
                S Offline
                SilverShalkin
                wrote on last edited by
                #7

                ok, ok... i thought you ment that i had to int inc... and i responded by that i have already... but i got the main point... i changed the for n loop to: for(int inc = 0;inc < NN;inc++) and it worked.. thanks! But i still have the problem with the unhandled... i didnt have time to change some things but, ill keep your responses in mind :) thanks for the help all! ~SilverShalkin :rose:

                C 1 Reply Last reply
                0
                • S SilverShalkin

                  ok... I've had problems with unhandled exception error before... but right when i fixed it, i kind of forgot what i did to fix it... this is my code... "im posting it all.. except my header file, just to make sure you know what im doing." #include #include "Class.h"//included header "Class.h" using namespace std; Name person; //class def person int NN = 1;//incriment char Names[21]; //total amount of names char SName[100]; //SetName function. bool Name::SetName(char* NName) { if(!NName) { return false; } int NLen = (strlen(NName) + 1); char *NewName = new char[NLen]; if(!NewName) { return false; } strcpy(NewName, NName); delete[] M_Name; M_Name = NewName; Names[NN] = *M_Name;//I think it has somthing to do with this NN++; return true; } const char* Name::GetName() { return M_Name; } //main int main() { for(;;)//main loop { gets(SName); person.SetName(SName); cout << person.GetName() << endl << endl; //print name that you just typed. for(int inc;inc < NN;inc++) //print all names. { cout << Names[inc] << endl; //error unhandled exception. }//end of print all names, loop. inc = 1; }//end main loop. return 0; } //end main //constructor & deconstructor //******************* Name::Name() { M_Name = NULL; } Name::~Name() { delete[] M_Name; } The error accured when i tried to set Names[NN] = *M_Name; the error feels like its at the tip of my tongue, yet i cant think of it. Thanks all for the help! ~SilverShalkin ps... how do you show a screen shot of your code? or post your code on CP with its background different... "is ther a place on the forum that shows how?" Thanks! :rose:

                  R Offline
                  R Offline
                  Renjith Ramachandran
                  wrote on last edited by
                  #8

                  please initialise the Loop Running variable mnc in to zero.. ie you must for(int mnc=0;mnc

                  S 1 Reply Last reply
                  0
                  • S SilverShalkin

                    ok, ok... i thought you ment that i had to int inc... and i responded by that i have already... but i got the main point... i changed the for n loop to: for(int inc = 0;inc < NN;inc++) and it worked.. thanks! But i still have the problem with the unhandled... i didnt have time to change some things but, ill keep your responses in mind :) thanks for the help all! ~SilverShalkin :rose:

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    Have you traced to see if your variable being referenced is NULL, or added a check to alert you to this case ? Checking if a variable is valid is always good - initialise all pointers to NULL, check if they are NULL before dereferencing or calling methods on them. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. And you don't spend much time with the opposite sex working day and night, unless the pizza delivery person happens to be young, cute, single and female. I can assure you, I've consumed more than a programmer's allotment of pizza, and these conditions have never aligned. - Christopher Duncan - 18/04/2002

                    1 Reply Last reply
                    0
                    • R Renjith Ramachandran

                      please initialise the Loop Running variable mnc in to zero.. ie you must for(int mnc=0;mnc

                      S Offline
                      S Offline
                      SilverShalkin
                      wrote on last edited by
                      #10

                      i did set it = to zero,... for the most part, that fixed it :) Thanks! ~SilverShalkin ps... I'll work the debugger on the NULL pointers :) Thanks! :rose:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups