Like Tree5Likes

Compile but wont run

This is a discussion on Compile but wont run within the C++ Programming forums, part of the General Programming Boards category; here is my code Code: // paycenter.cpp // Program to calculate overtime pay for employees #include <iostream> //for stream I/O ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    Compile but wont run

    here is my code

    Code:
    // paycenter.cpp
    // Program to calculate overtime pay for employees
    
    
    #include <iostream>											//for stream I/O
    #include <cstring>											//for strlen() and strcpy()
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    class Cemployee
    {
    public:
    	char* m_name;
    	double m_hours;
    	float m_wage;
    
    
    	Cemployee(char* str = "employee name", double hv = 1, float wv = 1):	m_hours(hv),m_wage(wv),m_name(str)
    	{
    		m_name = new char[ strlen(str) + 1];
    		strcpy_s(m_name, strlen(str) + 1, str);
    	}
    	~Cemployee(void)
    		{ delete[] m_name; }
    	
    	double gethours(void)
    	{
    		return m_hours;
    	}
    	float getwage(void)
    	{
    		return m_wage;
    	}
    	char* getname(void)
    	{
    		return m_name;
    	}
    };
    int main()
    {
    	char* name;
    	double hours;
    	float wage;
    	
    
    
    	cout << "Welcome to the Employee Pay Center" << endl << endl;
    
    
    	cout << "Enter the employee name = ";
    	cin >> name;
    	cout << endl << "Enter the hours worked = ";
    	cin >> hours;
    	cout << endl << "Enter his or her hourly wage = ";
    	cin >> wage;
    	
    	Cemployee emp1;
    	emp1.m_wage = wage;
    	emp1.m_hours = hours;
    	emp1.m_name = name;
    
    
    	cout << endl << endl << endl;
    		cout << "name " << emp1.getname();
    	cout << endl << "wage " << emp1.getwage();
    	cout << endl << "hours " << emp1.gethours() << endl;
    	system("pause");
    	return 0;
    		
    }
    This is just the start of the program, there is more to come. I am testing what I have so far and it compiles successfully, but when once prompted for the employee name, I enter the name, and then the program crashes.

    Please, I am new to C++, I do not need advanced fixed for the code, I just need to understand why it is crashing.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    Quote Originally Posted by amgleason83 View Post
    here is my code
    This is just the start of the program, there is more to come. I am testing what I have so far and it compiles successfully, but when once prompted for the employee name, I enter the name, and then the program crashes.
    Yep, it's because you didn't "new" any memory, for example:
    Code:
     
    #include <cstddef>
    
    name = new char[ BUFSIZ ];
    This associates BUFSIZ bytes with name. Now you can use it. But I also recommend against doing things like this:
    Code:
     cin >> name;
    It's too easy for the user to type too much and force the computer to write out of bounds.

    At a minimum, this:
    Code:
    #include <iomanip>
    
    cin >> setw( BUFSIZ -1 ) >> name;
    Be sure to delete[] the memory when you're done.
    stahta01 likes this.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Why aren't you using std::string instead of char* for the name? If you are going to use it the argument to the constructor should be const char* at least instead of just char*.

    Remove the initialization list part where you assign str to m_name. It serves no purpose since you throw away that value immediately once inside the constructor body.

    Your get functions should be declared const since they do not modify the object.

    The getname function should return a const char pointer, it is especially dangerous without this as it expose a raw pointer to the outside world that could otherwise have data copied to it.
    Salem and Elysia like this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    Update but still not working

    ok, here is how i updated the code, but i am still having the same problem. did i miss something you were saying?
    Code:
    // paycenter.cpp
    // Program to calculate overtime pay for employees
    
    
    #include <iostream>											//for stream I/O
    #include <cstring>											//for strlen() and strcpy()
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    
    
    class Cemployee
    {
    public:
    	char* m_name;
    	double m_hours;
    	float m_wage;
    
    
    	Cemployee(const char* str = "employee name", double hv = 1, float wv = 1):	m_hours(hv),m_wage(wv)
    	{
    		m_name = new char[ strlen(str) + 1];
    		strcpy_s(m_name, strlen(str) + 1, str);
    	}
    	~Cemployee(void)
    		{ delete[] m_name; }
    	
    	const double gethours(void) const
    	{
    		return m_hours;
    	}
    	const float getwage(void) const
    	{
    		return m_wage;
    	}
    	const char* getname(void) const
    	{
    		return m_name;
    	}
    };
    int main()
    {
    	char* name;
    	double hours;
    	float wage;
    	
    
    
    	cout << "Welcome to the Employee Pay Center" << endl << endl;
    
    
    	cout << "Enter the employee name = ";
    	cin >> name;
    	cout << endl << "Enter the hours worked = ";
    	cin >> hours;
    	cout << endl << "Enter his or her hourly wage = ";
    	cin >> wage;
    	
    	Cemployee emp1;
    	emp1.m_wage = wage;
    	emp1.m_hours = hours;
    	emp1.m_name = name;
    
    
    	cout << endl << endl << endl;
    		cout << "name " << emp1.getname();
    	cout << endl << "wage " << emp1.getwage();
    	cout << endl << "hours " << emp1.gethours() << endl;
    	system("pause");
    	return 0;
    		
    }

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    but i am still having the same problem
    Which implies that it is still crashing for you.
    But it doesn't crash for me.

    Anyway, why didn't you take the good advice of using a string, since this is C++ after all.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    I guess I am not understanding where to change to a string declaration that is not declared by char (character string?). all i am trying to do is create a Cemployee object that is initialized with user input. I just can't seem to get the character string to pass correctly. I dont know if the problem is in the class design or the main function. we were only given the chapter on how to pass character strings to derived classes, so trying to do so in a base class doesn't seem to work the same way. but im not sure how to initialize a character string as a class member through user input.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Maybe you don't know about the C++ string class. Teachers can be very dumb.

    However, I didn't notice before that you haven't declared any space for your name in main.
    It should be something like char name[100];
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    There is only one variable called name in the whole program and it's the reason your program is crashing. Really, name should be a std::string. So do you understand how to declare a variable? All you have to do is change the type.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    ok, I changed all the char types to string (if I changed only the type for name then the initialization of the emp1 object errors because type string cannot be cast to type char or char*. so i changed everything to type string, (after including the <string> header which took some investigation). now the program doesn't crash, but the getname function does not return a value, but gethours and getwage work fine. here is the updated code. (and yes, my teacher sucks. this is my final project, i have a 100 in this course and I am confused about declaring strings. i hate this school).
    Code:
    // paycenter.cpp
    // Program to calculate overtime pay for employees
    
    
    #include <iostream>											//for stream I/O
    #include <cstring>											//for strlen() and strcpy()
    #include <string>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    
    
    class Cemployee
    {
    public:
    	string m_name;
    	double m_hours;
    	float m_wage;
    
    
    	Cemployee(string str = "employee name", double hv = 1, float wv = 1):m_hours(hv),m_wage(wv)
    	{}
    
    
    	~Cemployee(void)
    		{}
    	
    	const double gethours(void) const
    	{
    		return m_hours;
    	}
    	const float getwage(void) const
    	{
    		return m_wage;
    	}
    	const string getname(void) const
    	{
    		return m_name;
    	}
    };
    int main()
    {
    	string name = "employee name";
    	double hours;
    	float wage;
    	
    	cout << endl << "Welcome to the Employee Pay Center" << endl << endl;
    
    
    	cout << "Enter the employee name = ";
    	cin >> name;
    	cout << endl << "Enter the hours worked = ";
    	cin >> hours;
    	cout << endl << "Enter his or her hourly wage = ";
    	cin >> wage;
    	
    	Cemployee emp1(name, hours, wage);
    	
    	cout << endl << endl << endl
    	<< "Employee 1" << endl
    	<<"name = " << emp1.getname()
    	<< endl << "wage = " << emp1.getwage()
    	<< endl << "hours = " << emp1.gethours() << endl << endl;
    
    
    
    
    	system("pause");
    	return 0;
    		
    }

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're not setting m_name in the CEmployee constructor.
    Also, if name can have a space in it (first and last name) you'll want to use getline in main to read it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    Aha! Its finally starting to do what I want it to. I was confused because earlier someone said to remove the m_name initialization of str. ​ Thank you all very much.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by amgleason83 View Post
    Aha! Its finally starting to do what I want it to. I was confused because earlier someone said to remove the m_name initialization of str. ​ Thank you all very much.
    Yes, and my comment did serve a purpose when m_name was a char pointer and your constructor was responsible for allocating memory for the data member. Once you switched to a std::string container for the name however the need for that returned.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Actually in function main() you were not allocating memory to char* that is why it was getting crashed...

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Read all about Rule of three (C++ programming) - Wikipedia, the free encyclopedia

    I caught someone at work today who didn't follow this. They did exactly what you have done here, and didn't think they needed the copy-constructor or assignment operator because they were never copying instances of the class. It turned out that they were unintentionally copying them, which I luckily spotted during code review, and they should now have these as private and unimplemented.

    If your code has a destructor without a copy-constructor and assignment operator, then it is broken, end of story.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by iMalc
    If your code has a destructor without a copy-constructor and assignment operator, then it is broken, end of story.
    Not quite end of story: there is an exception where the destructor is user defined solely to declare it virtual for a polymorphic base class. In such a case, the compiler generated copy constructor and assignment operator may well still be correct.
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why wont this compile:
    By ItchyBob in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2011, 12:25 PM
  2. Why wont this compile?
    By Dieselfreak in forum C Programming
    Replies: 5
    Last Post: 12-13-2010, 10:37 AM
  3. WTF MS VC++ wont compile
    By EvilPickles in forum Tech Board
    Replies: 1
    Last Post: 09-23-2006, 09:36 AM
  4. Why wont anything compile?
    By Crazyflanger in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2003, 01:18 AM
  5. Wont Compile In VS.NET
    By Troll_King in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21