Reputation: 73
When I am trying to assign a member variable, which is const char*, it just ends the program. However, if I get some cin inputs before I assign a member variable, it works. this is my NameCard.h
class NameCard {
private:
const char* name;
const char* companyName;
const char* phoneNumber;
enum grade{CLERK, SENIOR, ASSIST, MANAGER};
grade my_grade;
NameCard* nextCard;
NameCard* head;
public:
NameCard(const char*, const char*, const char*, int);
void setName();
void setCompanyName();
void setPhoneNumber();
void setMyGrade();
void setHead(NameCard*);
void setNextCard(NameCard*);
void findInfo(char*);
void printAll();
};
This is main.cpp
#include <iostream>
#include <cstring>
#include "NameCard.h"
using namespace std;
NameCard::NameCard(const char* a, const char* b, const char* c, int d){
name = a;
companyName = b;
phoneNumber = c;
switch(d){
case CLERK: my_grade = CLERK; break;
case SENIOR: my_grade = SENIOR; break;
case ASSIST: my_grade = ASSIST; break;
case MANAGER: my_grade = MANAGER; break;
}
cout << name << " " << companyName << " " << phoneNumber << " " << my_grade << endl;
}
void NameCard::setName(){
char* name;
cout << "type name." << endl;
cin >> name;
this->name = name;
cout << this->name << endl;
}
void NameCard::setCompanyName(){
char* company_name;
cout << "type company name." << endl;
cin >> company_name;
this->companyName = company_name;
cout << this->companyName << endl;
}
void NameCard::setPhoneNumber(){
char* phone_number;
cout << "type phone number." << endl;
cin >> phone_number;
phoneNumber = phone_number;
cout << phoneNumber;
};
void NameCard::setMyGrade(){
int input_grade;
cout << "type position. 1)CLERK 2) SENOIR 3) ASSIST 4)MANAGER" << endl;
cin >> input_grade;
switch(input_grade){
case CLERK: my_grade = CLERK; break;
case SENIOR: my_grade = SENIOR; break;
case ASSIST: my_grade = ASSIST; break;
case MANAGER: my_grade = MANAGER; break;
}
cout << input_grade;
};
void NameCard::setHead(NameCard* head){
this->head = head;
};
void NameCard::setNextCard(NameCard* next){
this->nextCard = next;
}
void NameCard::findInfo(char* target_name){
NameCard* temp;
temp = head;
while(temp != NULL){
if(temp->name == target_name){
cout << "name : " << temp->name << endl;
cout << "companyName : " << temp->companyName << endl;
cout << "phoneNumber : " << temp->phoneNumber << endl;
cout << "grade : " << temp->my_grade << endl;
break;
}
temp = temp->nextCard;
}
};
void NameCard::printAll(){
NameCard* temp;
temp = head;
while(temp!= NULL){
cout<< "----------------------------" << endl;
cout << "name : " << temp->name << endl;
cout << "companyName : " << temp->companyName << endl;
cout << "phoneNumber : " << temp->phoneNumber << endl;
cout << "grade : " << temp->my_grade << endl;
temp = temp->nextCard;
}
}
int main(){
int index = 0;
NameCard* head = new NameCard("test", "test", "test", 3);
NameCard* bef = NULL;
// char input[50];
// cout << "nmae ";
// cin >>input;
head->setName();
head->setPhoneNumber();
head->setCompanyName();
head->setMyGrade();
return 0;
}
And the problem is here. This is not working. The program ends right after it gets name from setName(). It just ends at the setName() cin >> name. it did not print name after then, and stopped.
int main(){
int index = 0;
NameCard* head = new NameCard("test", "test", "test", 3);
NameCard* bef = NULL;
// char input[50];
// cout << "nmae ";
// cin >>input;
head->setName();
head->setPhoneNumber();
head->setCompanyName();
head->setMyGrade();
return 0;
}
However, this is working fine. it gets the test input and executes all the code below.
int main(){
int index = 0;
NameCard* head = new NameCard("test", "test", "test", 3);
NameCard* bef = NULL;
char input[50];
cout << "test input ";
cin >>input;
head->setName();
head->setPhoneNumber();
head->setCompanyName();
head->setMyGrade();
return 0;
}
Upvotes: 0
Views: 1160
Reputation: 584
You are writing to memory you didn't allocate and most likely don't own. Check this for example
void NameCard::setName(){
char* name;
cout << "type name." << endl;
cin >> name;
this->name = name;
cout << this->name << endl;
}
You declare char* name
and try to read from cin
into it. But name
is just an uninitialized pointer.
One fix may be to declare name
as an array, which also decays to a pointer (so it can be used as a pointer), but does have associated memory. Like this
char name[50];
Or if you want it to be dynamically allocated
char* name = new char[50];
// Use it and when you are done delete it
// Never forget to release memory allocated with new or you get a memory leak
delete[] name;
The key point is that the pointer needs to point to some memory block you know you can use. Note that in the example I allocated 50 chars, you have to make sure you never use (read/write) past the allocated memory, or you get the same error you have been getting and potentially program termination.
A safer alternative is to use std::string
, which automatically handles memory for you, so you don't have to mind about allocation and releasing.
You could do something like
std::string name;
std::cin >> name;
Upvotes: 2