Advertisement

Run-time Error

Started by April 06, 2002 12:54 PM
6 comments, last by XtremeIdentity 22 years, 7 months ago
Run time error...!
  
#include <iostream>
using namespace std;

class something
	{
	private:
		char * name;
	public:
		void getprintname();
	}obj1;

int main()
	{
	obj1.getprintname();
	return 0;
	}

void something::getprintname()
	{
	cout << "Enter name :";
	cin >> name;
	cout << "\n\nName - " << name;
	}
  
-------------------------- ->Take it to the Xtreme!<- --------------------------
--------------------------->Take it to the Xtreme!<---------------------------
You don''t allocate memory, so the insertion operator attempts to write to memory not owned by your application. I suggest you use std::strings for now, and learn how to manipulate C-style strings later (and only if you really need them).


  #include <iostream>#include <string>using namespace std;class something{public:  void getprintname();private:  string name;} obj1;//int main(){  obj1.getprintname();  return 0;}//void something::getprintname(){  cout << "Enter a name: ";  cin >> name;  cout << "You entered \"" << name << "\"." << endl;}  


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
Advertisement
Maybe the problem is in your library. Did you add all *.h files to your project?
Or, your compiler is set wrong.

____________
BEST REGARDS
ALEX SALO
____________
________________Alex Saloalexsalo@ukr.net
Try

cin >> *name;

If I remember correctly.

,Jay
Ignore the two above posts, they know not what they are talking about. Oluseyi is 100% correct.

An easy way around this is to use "char name[128];", which will provide a buffer capable of holding a sting which is 127 characters long.

I've never ever used the stl apart from the fstream lot ( call be backwards ), so I can't comment on how easy they are.

Death of one is a tragedy, death of a million is just a statistic.

[edited by - python_regious on April 6, 2002 3:59:47 PM]
If at first you don't succeed, redefine success.
There is such thing as a ''string'' data type in C++?!? Ok, I''ll try it...

--------------------------
->Take it to the Xtreme!<-
--------------------------
--------------------------->Take it to the Xtreme!<---------------------------
Advertisement
Wait, is the only way to use a char * is to make a char array first?

--------------------------
->Take it to the Xtreme!<-
--------------------------
--------------------------->Take it to the Xtreme!<---------------------------
quote: Original post by XtremeIdentity
Wait, is the only way to use a char * is to make a char array first?

No, but there are enough nuances to its use that I suggest you use std::string until you''re ready to interact with legacy APIs (Win32, for example) at which point you''ll need to learn about static and dynamic arrays and strings as character arrays. For general purpose dynamic arrays, please use std::vector.


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement