C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Pointer to Char Problem Posted by Danish Kamran on 30 Dec 2008 at 3:49 AM
My Question is about code given below

#include <iostream>
using namespace std;

int main()
{
char *Name;
cout << " Enter your name with space(E.g Joe Mark)\n";
cin.getline( name, 15);
cout << line;
system("pause");
return 0;
}
Question

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
Report
Re: Pointer to Char Problem Posted by AsmGuru62 on 30 Dec 2008 at 5:20 AM
: My Question is about code given below
:
: #include <iostream>
: using namespace std;
:
: int main()
: {
: char *Name;//you need to allocate some room here!
: cout << " Enter your name with space(E.g Joe Mark)\n";
: cin.getline( name, 15);
: cout << line;
: system("pause");
: return 0;
: }
: Question
:
: 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
:
Report
Re: Pointer to Char Problem Posted by aramonkg101 on 6 Jan 2009 at 8:10 AM
First you declare
char* Name;

while you use
cin.getline(name, 15);

Name and name are different, but I guess you have corrected it.
Also
cout << line;

Where do you declare line? 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...
char* name = (char*) malloc(SIZE_IN_BYTES_OF_MEMORY_REQUESTED);

Do not forget to free it up when done using it.
free(name);


Finally, since you use using C++ you are better off using C++ facilities, like the string class. So you could make these changes
 #include <iostream>
+#include <string>
 using namespace std;
 
 int main()
 {
-  char *name = (char*) malloc(20);
+  string name;
   cout << " Enter your name with space(E.g Joe Mark)\n";
-  cin.getline(name, 20);
+  getline(cin, name);
   cout << name;
-  free(name);
   system("pause");
   return 0;
 }

Report
Re: Pointer to Char Problem Posted by Lundin on 7 Jan 2009 at 7:16 AM
: 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.

Report
Re: Pointer to Char Problem Posted by aramonkg101 on 8 Jan 2009 at 3:52 PM
True that! Mae culpa. Which makes the code look something like this in the non-string class program.
 #include <iostream>
 using namespace std;
 
 int main()
 {
-  char *name = (char*) malloc(20);
+  char *name = (char*) new char[20];
   cout << " Enter your name with space(E.g Joe Mark)\n";
   cin.getline(name, 20);
   cout << name;
-  free(name);
+  delete [] name;
   system("pause");
   return 0;
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.