Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Pointer to Char Problem

My Question is about code given below

#include
using namespace std;

int main()
{
char *Name;
cout << " Enter your name with space(E.g Joe Mark)
";
cin.getline( name, 15);
cout << line;
system("pause");
return 0;
}
[color=Green][b]Question[/b][/color]

whenever I take a name from key board along with spaces the program crashes. However If I ismply enter name without spaces It works fine.
Why is this happening.

looking for kind reply.

Regards,
Danish Kamran

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : My Question is about code given below
    :
    : #include
    : using namespace std;
    :
    : int main()
    : {
    : char *Name;[color=Red]//you need to allocate some room here![/color]
    : cout << " Enter your name with space(E.g Joe Mark)
    ";
    : cin.getline( name, 15);
    : cout << line;
    : system("pause");
    : return 0;
    : }
    : [color=Green][b]Question[/b][/color]
    :
    : whenever I take a name from key board along with spaces the program
    : crashes. However If I ismply enter name without spaces It works fine.
    : Why is this happening.
    :
    : looking for kind reply.
    :
    : Regards,
    : Danish Kamran
    :
  • aramonkg101aramonkg101 Posts: 12Member
    First you declare
    [code]
    char* Name;
    [/code]
    while you use
    [code]
    cin.getline(name, 15);
    [/code]
    [italic]Name[/italic] and [italic]name[/italic] are different, but I guess you have corrected it.
    Also
    [code]
    cout << line;
    [/code]
    Where do you declare [italic]line[/italic]? I doubt this code compiled in that form...

    So to correct your code in this form follow the suggestion of allocating memory for the pointer. For example...
    [code]
    char* name = (char*) malloc(SIZE_IN_BYTES_OF_MEMORY_REQUESTED);
    [/code]
    Do not forget to free it up when done using it.
    [code]
    free(name);
    [/code]

    Finally, since you use using C++ you are better off using C++ facilities, like the string class. So you could make these changes
    [code]
    #include <iostream>
    [color=Green]+#include [/color]
    using namespace std;

    int main()
    {
    [color=Red]- char *name = (char*) malloc(20);[/color]
    [color=Green]+ string name;[/color]
    cout << " Enter your name with space(E.g Joe Mark)
    ";
    [color=Red]- cin.getline(name, 20);[/color]
    [color=Green]+ getline(cin, name);[/color]
    cout << name;
    [color=Red]- free(name);[/color]
    system("pause");
    return 0;
    }
    [/code]
  • LundinLundin Posts: 3,711Member
    : Finally, since you use using C++ you are better off using C++ facilities, like the string class.

    This is a good advice. Therefore, use new and delete instead of malloc/free. The two methods are not compatible with each other and using malloc in C++ programs can therefore cause serious bugs in case you accidently try to delete[] memory allocated with malloc.

  • aramonkg101aramonkg101 Posts: 12Member
    True that! Mae culpa. Which makes the code look something like this in the non-string class program.
    [code]
    #include
    using namespace std;

    int main()
    {
    [color=Red]- char *name = (char*) malloc(20);[/color]
    [color=Green]+ char *name = (char*) new char[20];[/color]
    cout << " Enter your name with space(E.g Joe Mark)
    ";
    cin.getline(name, 20);
    cout << name;
    [color=Red]- free(name);[/color]
    [color=Green]+ delete [] name;[/color]
    system("pause");
    return 0;
    }
    [/code]
Sign In or Register to comment.