Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Another exercise i am stuck with (http://www.go4expert.com/forums/exercise-i-stuck-t7048/)

boschow 26Oct2007 02:10

Another exercise i am stuck with
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:

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:


#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')
    memory = info;
    info = memory;
    *point = *memory;
    if (count_char > 0) return memory;
    else return '\0';

int main()
    char *info = new char[200];
    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,

shabbir 26Oct2007 08:38

Re: Another exercise i am stuck with
Try giving good titles for better responses.

dssrrjy 29Oct2007 00:03

Re: Another exercise i am stuck with

Originally Posted by shabbir
Try giving good titles for better responses.

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.

All times are GMT +5.5. The time now is 14:26.