![]() |
| | #1 |
| Registered User Join Date: Jul 2002
Posts: 140
| Am I going insane? How do you input strings? I want the user of my program to input a string using the standard input device. Then I want to test that string using if statements. This is what I have so far, but for some reason I get an illegal error when I try run the .exe file. Code: char* input; std::cin >> input; should I use the <string> library header? the new/delete operators? am i really crazy?
__________________ AOL: GrNxxDaY IDE: Dev-C++ Beta 5 (v4.9.4.1) Project: Eye of Sahjz (text-RPG) If you think I may need help, please IM me. |
| GrNxxDaY is offline | |
| | #2 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| try this, Code: char* input = new char[256]; cin >> input; |
| The Dog is offline | |
| | #3 |
| Registered User Join Date: Jul 2002
Posts: 140
| I would, but here's the problem Code: void getInput() // shortened version
{
char* input = new char[40];
std::cin >> input;
if (input == "err")
doIt();
// bunch of other if statements
}
Anyone know how to slove this problem?
__________________ AOL: GrNxxDaY IDE: Dev-C++ Beta 5 (v4.9.4.1) Project: Eye of Sahjz (text-RPG) If you think I may need help, please IM me. |
| GrNxxDaY is offline | |
| | #4 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| then delete it when you are done with it. |
| The Dog is offline | |
| | #5 |
| Registered User Join Date: Jul 2002
Posts: 140
| it has local scope, i'd have to delete it inside that function. but i can't because after the function uses the new keyword, it executes a different function, never returning to that orginal copy of getInput();
__________________ AOL: GrNxxDaY IDE: Dev-C++ Beta 5 (v4.9.4.1) Project: Eye of Sahjz (text-RPG) If you think I may need help, please IM me. |
| GrNxxDaY is offline | |
| | #6 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| then rather declare 'input' as an array. Code: #define MAX_ARRAY 256
void getInput() // shortened version
{
char* input = new char[MAX_ARRAY];
std::cin >> input;
if (input == "err")
doIt();
// bunch of other if statements
}
|
| The Dog is offline | |
| | #7 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| sorry bout that should've been Code: #define MAX_ARRAY 256
void getInput() // shortened version
{
char input[MAX_ARRAY];
std::cin >> input;
if (input == "err")
doIt();
// bunch of other if statements
}
|
| The Dog is offline | |
| | #8 | |
| Just because Join Date: Jan 2002
Posts: 2,502
| Quote:
Code: if (input == "err")
doIt();
//edit: you might also want to know that Code: char* szsomething = new char[40]; cin >> szsomething; Last edited by ygfperson; 07-27-2002 at 08:27 PM. | |
| ygfperson is offline | |
| | #9 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| i think that ygfperson misunderstood what it is you're trying to do GrNxxDaY. to delete a pointer, you need the pointer in the first place. to allow doIt() to delete the pointer, you have to pass the pointer to the function. |
| The Dog is offline | |
| | #10 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| if you're going to new an object, and you want to delete it in another function, then you'll have to pass a pointer to it to the function. |
| The Dog is offline | |
| | #11 |
| Guest Join Date: Aug 2001
Posts: 5,034
| The Dog gave you the correct answer. So do: void getInput() // shortened version { char input[40]; std::cin >> input; if ( strcmp(input, "err") == 0) doIt(); // bunch of other if statements } |
| Sebastiani is offline | |
| | #12 |
| Registered User Join Date: Jul 2002
Posts: 140
| Will this work? Code: void deleteDynam(char* victim)
{
delete [] *victim;
}
void getInput()
{
char* input = new char[40];
deleteDynam(&input);
}
__________________ AOL: GrNxxDaY IDE: Dev-C++ Beta 5 (v4.9.4.1) Project: Eye of Sahjz (text-RPG) If you think I may need help, please IM me. |
| GrNxxDaY is offline | |
| | #13 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| No, but this will Code: void deleteDynam(char* victim)
{
delete [] victim;
}
void getInput()
{
char* input = new char[40];
deleteDynam(input);
}
|
| The Dog is offline | |
| | #14 |
| Registered User Join Date: Jul 2002
Posts: 140
| ok, the heap it is. (for sure now, dont make me change my mind!) hehe, thakns guys!!
__________________ AOL: GrNxxDaY IDE: Dev-C++ Beta 5 (v4.9.4.1) Project: Eye of Sahjz (text-RPG) If you think I may need help, please IM me. Last edited by GrNxxDaY; 07-27-2002 at 08:51 PM. |
| GrNxxDaY is offline | |
| | #15 |
| Registered User Join Date: Jul 2002
Posts: 140
| Code: void deleteDynam(char* victim)
{
delete [] victim;
}
Code: void deleteDynam(char* victim)
{
delete [] *victim;
}
__________________ AOL: GrNxxDaY IDE: Dev-C++ Beta 5 (v4.9.4.1) Project: Eye of Sahjz (text-RPG) If you think I may need help, please IM me. |
| GrNxxDaY is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| fscanf in different functions for the same file | bchan90 | C Programming | 5 | 12-03-2008 09:31 PM |
| Printing Length of Input and the Limited Input | dnguyen1022 | C Programming | 33 | 11-29-2008 04:13 PM |
| customer input | buckwheat88 | C Programming | 6 | 11-21-2005 08:01 PM |
| EOF messing up my input stream? | Decrypt | C++ Programming | 4 | 09-30-2005 03:00 PM |
| Question about File Input | ChristianTool | C Programming | 12 | 04-25-2004 02:40 PM |