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.



4Likes
LinkBack URL
About LinkBacks



