Like Tree4Likes
  • 3 Post By Elkvis
  • 1 Post By Elysia

Program Stops Responding.

This is a discussion on Program Stops Responding. within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem with my program deciding to stop responding. The program is as follows: Code: #include <iostream.h> main() ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    14

    Program Stops Responding.

    I'm having a problem with my program deciding to stop responding. The program is as follows:

    Code:
    #include <iostream.h>
    main()
    {
        char * q = new char [100];
        char * i = new char [30];
        char * a = new char [30];
        q = new char [100]; i = new char [30]; a = new char [30];
        q = "What is 2 + 2?\n";
        cout << q;
        cin >> i;
        a = "4";
        if (i == a)
            cout << "Correct.\n";
        else
            cout << "No, answer is " << a << ".\n";
        delete q; delete i; delete a;
    }

    The program has a simple purpose: the goal is to have it be a sort of study guide. It will also be the first program I use new or delete in.

    I had a similar problem awhile back when I learned the basics of normal C. I would forget the ampersand before my inputs in scanf and it would stop responding in the exact same way, but I didn't use scanf here.

    I'm not certain, ut I think it's a similar problem with using new. Removing the delete command didn't fix it.

    If it matters, I use the Digital Mars compiler, which is run from the command line, cmd.

    EDIT: I have made a small modification so that the first 3 lines no longer make it so that memory for the strings is allocated twice, it now says this:
    [CODE]char * q; char * i; char * a;[CODE]
    Also, I thought it might help to mention that I don't get any errors or warnings. I don't see a problem, but there clearly is one. I don't tthink my program would just quit responding like that.
    Last edited by KittenAqua; 12-15-2011 at 06:29 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,456
    there's a number of things that are a little bit off about this program.

    1. you cannot compare c-style strings (char*) using the == operator. you need to use strcmp().

    2. when you delete an array of objects, you cannot simply say delete q; you must say delete[] q; so it knows to delete ALL the elements of the array.

    3. you're calling new twice on each array, leaking the memory from the first time.

    4. when you say q = "What is 2 + 2?\n"; you are leaking the memory allocated with new. if you wish to assign data to a pre-allocated char buffer, you must use strcpy().

    5. you will also likely get compiler warnings with the assignment in line 8, because C++ does not like to assign a const char array to a non-const char pointer.

    6. main() should be int main(). and at the end you need to have a return value. C++ does not allow implicit return value types in function declarations.

    7. <iostream.h> should be simply <iostream> using iostream.h is non-standard, and should be avoided at all costs. if you are using a compiler (borland) that does not accept <iostream> then you need to find a modern compiler like visual C++ or G++.
    Last edited by Elkvis; 12-15-2011 at 09:02 PM.
    iMalc, Elysia and KittenAqua like this.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    To add, line 8 is perfectly fine from a language standpoint. Due to backwards compatibility, it will compile without warnings, but it's wrong , as Elkvis has pointed out.

    >>cin >> i;
    Completely and utterly unsafe. Prone to buffer overflows. Never do this! Use std::string and std::getline instead!

    >>a = "4";
    Same as line 8!

    And finally, time to upgrade. Get rid of that old compiler and get a modern one.
    Visual C++ (Visual Studio is the accompanying IDE) and GCC are good choices.
    KittenAqua likes this.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    Thanks for your help, both of you. I'll be making the appropriate adjustments to the program, and hopefully it will respond.

    One question though, what's wrong with my compiler?

    EDIT: Apparently my compiler doesn't accept plain iostream. I'm downloading Visual C++ now.

    EDIIT: I've narrowed it down to the compiler only having problems with lvalues and being unable to open <iostream>. Now I'm just going to wait until visual C++ is done installing.
    Last edited by KittenAqua; 12-16-2011 at 05:50 AM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Quote Originally Posted by KittenAqua View Post
    One question though, what's wrong with my compiler?

    EDIT: Apparently my compiler doesn't accept plain iostream. I'm downloading Visual C++ now.
    That by itself says everything. The compiler is so old it doesn't even support the C++03 standard, much less the newest C++11 standard.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,456
    as of right now, GCC/G++ has the most complete support for C++11, although it's still lacking a few things I want to try out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program suddenly stops working.
    By KittenAqua in forum C Programming
    Replies: 5
    Last Post: 10-18-2011, 05:45 AM
  2. MessageBox stops program flow
    By Ducky in forum Windows Programming
    Replies: 3
    Last Post: 06-20-2011, 10:40 PM
  3. Program stops working as soon as I input value?..
    By darkmagic in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2011, 01:18 AM
  4. program stops before EOF
    By doia in forum C Programming
    Replies: 2
    Last Post: 03-22-2010, 12:10 PM
  5. Program stops executing
    By bargomer in forum C++ Programming
    Replies: 16
    Last Post: 04-26-2008, 12:05 PM

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