1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Another exercise i am stuck with

Discussion in 'C++' started by boschow, Oct 25, 2007.

  1. boschow

    boschow New Member

    Joined:
    Feb 22, 2007
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Hello friends,
    i have a new problem with another exercise. It states like this:

    Imagine that we have a program that requires the informations of the user. for this purpose write a function Insert, that will look after the users informations. Because the function has to be general, its parameter should be a string, that has to be printed on the screen. If the user didn't write any informations the function returns NULL (char *Insert( char* )). The usage of the function is the following:
    Code:
    char *name;
            name = Insert("Write your name . . .");
    // . . .
    delete name
    
    (TIP: In the function use a static field (200 index). When the user writes the text, first of all discover the number of index. Reserve enough space, copy the string and return the pointer on the reserved space).

    My code looks like this:

    Code:
    //POGL8_vaja2.cpp
    #include <iostream>
    using namespace std;
    char clear_array(char *info)
    {
         int i;
         for (i==0; i < 200; i++)
         {
             info[i] = 0;
         }
    }
    
    char *feelIn(char *info)
    {
         int count_char;
         char *point = info;
         char *memory = new char[200];
         while (*point != '\0')
         {
             point++;
             count_char++;
         }
         memory[count_char];
         memory = info;
         info = memory;
         *point = *memory;
         if (count_char > 0) return memory;
         else return '\0';
    }
    
    int main()
    {
        char *info = new char[200];
        clear_array(info);
        cout << "Write down your name!" << endl;
        cin >> info;
        char *space = feelIn(info);
        cout << "Your name is " << space << endl;
        delete info;
    }
    
    I have two errors. If I just press the button enter the application still waits for the informations of the users.
    the second error is that if i write a name for instance Mark
    the program returns MarkM.

    I don't know what i did wrong thats why i am asking for your help.

    thanks a lot
    best regards,
    BoSCHoW.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Try giving good titles for better responses.
     
  3. dssrrjy

    dssrrjy New Member

    Joined:
    Oct 28, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Regarding the second question (printing wrong name), i think you need to comment out the below line.

    *point = *memory;

    Secondly, in the for loop you should use i=0 instead of i==0 (may be typing error).
    One more thing is clear_array function should not return anything. You should use void clear_array for it.
     

Share This Page