Am I going insane? How do you input strings?

This is a discussion on Am I going insane? How do you input strings? within the C++ Programming forums, part of the General Programming Boards category; I am having a brain fart and can't seem to get this to work right. I want the user of ...

  1. #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.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    try this,

    Code:
    char* input = new char[256];
    cin >> input;

  3. #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.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    then delete it when you are done with it.

  5. #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.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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 
    }

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    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.

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.

  10. #10
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    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
    }



    ITSA
    Socket Library!

  12. #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.

  13. #13
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    No, but this will
    Code:
    void deleteDynam(char* victim)
    {
       delete [] victim;
    }
    
    void getInput()
    {
       char* input = new char[40];
       deleteDynam(input);
    }

  14. #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!!
    Last edited by GrNxxDaY; 07-27-2002 at 08:51 PM.
    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.

  15. #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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  3. customer input
    By buckwheat88 in forum C Programming
    Replies: 6
    Last Post: 11-21-2005, 07:01 PM
  4. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  5. Question about File Input
    By ChristianTool in forum C Programming
    Replies: 12
    Last Post: 04-25-2004, 02:40 PM

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