Why does the program crash?

This is a discussion on Why does the program crash? within the C++ Programming forums, part of the General Programming Boards category; Code: char* sNameIN; for(;;) { cout<<"Enter a name:"<<endl; for(int i=0;i<4;i++) { cout<<sNames[i]<<endl; } cin>>sNameIN; bool bCMP=false; for(int ind=0;ind<4;ind++) { bCMP=!(strcasecmp(sNameIN,sNames[ind])); ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Why does the program crash?

    Code:
    char* sNameIN;
    for(;;)
    {
    cout<<"Enter a name:"<<endl;
        for(int i=0;i<4;i++)
        {
        cout<<sNames[i]<<endl;
        }
    cin>>sNameIN;
    bool bCMP=false;
        for(int ind=0;ind<4;ind++)
        {
        bCMP=!(strcasecmp(sNameIN,sNames[ind]));
        iCount=ind;
        if(bCMP==1)
        {
        goto CharSelect;//"gotoEN"
        }
        }
    }
    I don't understand. Earlier in the day, I had this code in my file, it
    compiled without error, and the program ran like it should have. Now,
    when I try to run it, as soon as it hits the 'cin' line, and the user
    hits 'enter' the program crashes. Why, and how do I get around this
    P.I.T.A.? Keep in mind that this is of course only the relevant part of the program...

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    can't cin into a pointer directly.. but you can cin into a string or cstring and then make your pointer assignment.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    *sNameIN; is not pointing anything so you cannot modify to where it is pointing.

  4. #4
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    You never assign your char* pointer to anything at first....so it could be pointing anywhere in memory. Also, gotos could get you killed around these parts.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    sNameIN was not assigned to anything initiall because I had thought that C++ could accomodate for different sized strings entered by the user. (More than nine causes goofy outputs and
    more than 13 causes the prog to crash). I had worried that if I initialized it to null value then memory would only accomodate space for one char, which is not good. Yes, I AM a noob.

    As for that goto, I figured it was better to use that than to use
    two break statements to exit the inner and outer loops. Really, why are gotos seen as so bad? This is genuine curiosity...

    What I had after that that I didn't post was a switch referring to
    iCount, for values between and including 0 to 3, since only those three values could be output from the for loop, I didn't include any outside contingencies. What I noticed was that when I had
    declared an instance of the class [stats] within that switch, the program crashed upon the cin input. I removed it, and the switch worked fine.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    What I have noticed is that if I use 'cin' to a char*, that does
    'something' to the program that causes it to crash. Later in the program I included another 'cin' but no matter what that would crash the program. If I use 'cin' to a STRING type, I don't have a problem, but the function strcasecmp does not accept STRING on the compiler; only char*. The question is, how does one get around that and not risk crashing the program?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    1. Use a std::string for input sNameIN

    2a. bCMP=!(strcasecmp(sNameIN.c_str(),sNames[ind]));
    2. Read this http://www.devx.com/getHelpOn/10Minu...16972/0/page/2
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Not sure what you mean by that......I'm not that educated. I know you are referring to a class, though. The error message I usually get says 'cannot convert std::string from XXXX' or something to that effect.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Code:
    std::string sNameIN;
    cin >> sNameIN;
    bCMP=!(strcasecmp(sNameIN.c_str(),sNames[ind]));
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 09:17 PM
  2. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 05:06 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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