C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-27-2002, 07:45 PM   #1
Registered User
 
GrNxxDaY's Avatar
 
Join Date: Jul 2002
Posts: 140
Am I going insane? How do you input strings?

I am having a brain fart and can't seem to get this to work right.
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;
am I going crazy? This seems to simple to be stuck on

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   Reply With Quote
Old 07-27-2002, 07:59 PM   #2
Registered User
 
The Dog's Avatar
 
Join Date: May 2002
Location: Cape Town
Posts: 777
try this,

Code:
char* input = new char[256];
cin >> input;
The Dog is offline   Reply With Quote
Old 07-27-2002, 08:10 PM   #3
Registered User
 
GrNxxDaY's Avatar
 
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 
}
I can't put the delete [] thingy anywhere cause right after the user inputs a string, another function executes(based on the if statements), and the function never reaches the end. Also, this function gets called alot.

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   Reply With Quote
Old 07-27-2002, 08:13 PM   #4
Registered User
 
The Dog's Avatar
 
Join Date: May 2002
Location: Cape Town
Posts: 777
then delete it when you are done with it.
The Dog is offline   Reply With Quote
Old 07-27-2002, 08:15 PM   #5
Registered User
 
GrNxxDaY's Avatar
 
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   Reply With Quote
Old 07-27-2002, 08:22 PM   #6
Registered User
 
The Dog's Avatar
 
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 
}
you also could do it by passing the char* to each function by reference, then you could delete it in the function.
The Dog is offline   Reply With Quote
Old 07-27-2002, 08:23 PM   #7
Registered User
 
The Dog's Avatar
 
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   Reply With Quote
Old 07-27-2002, 08:23 PM   #8
Just because
 
ygfperson's Avatar
 
Join Date: Jan 2002
Posts: 2,502
Quote:
Originally posted by GrNxxDaY
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();
delete doesn't have a scope, afaik. any space allocated with 'new' is global and won't unallocate unless there's a delete operator or the program closes.

Code:
if (input == "err")
     doIt();
input is a pointer. "err" is a const char pointer, pointing to a different string which might happen to have the same contents. use strcmp(), or use string classes.

//edit: you might also want to know that
Code:
char* szsomething = new char[40];
cin >> szsomething;
will input only everything up to a space. to do a whole line, use cin.getline().

Last edited by ygfperson; 07-27-2002 at 08:27 PM.
ygfperson is offline   Reply With Quote
Old 07-27-2002, 08:32 PM   #9
Registered User
 
The Dog's Avatar
 
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   Reply With Quote
Old 07-27-2002, 08:38 PM   #10
Registered User
 
The Dog's Avatar
 
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   Reply With Quote
Old 07-27-2002, 08:42 PM   #11
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 07-27-2002, 08:43 PM   #12
Registered User
 
GrNxxDaY's Avatar
 
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   Reply With Quote
Old 07-27-2002, 08:46 PM   #13
Registered User
 
The Dog's Avatar
 
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   Reply With Quote
Old 07-27-2002, 08:47 PM   #14
Registered User
 
GrNxxDaY's Avatar
 
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   Reply With Quote
Old 07-27-2002, 09:47 PM   #15
Registered User
 
GrNxxDaY's Avatar
 
Join Date: Jul 2002
Posts: 140
Code:
void deleteDynam(char* victim)
{
   delete [] victim;
}
Do I need a * before victim there? Like so...
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:46 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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