for first b->readData() i get
Enter Student name:
Enter Student surname:
Enter Student ID:
but for second on i get
Enter Student name: Enter Student surname:
Enter Student ID:
why I'm getting it together for second one and not separate as in first? and how to get it to be separate.
Pls keep in mind that its explicit asked from me to use char* and also object Student in main must be dynamic
1 2 3 4 5 6 7 8
// untitled.h
class Student {
char* name = newchar[30];
char* surname = newchar[30];
int ID;
public:
void readData();
};
try cin, or getline, or whatever instead of fgets.
if allowed to, you may also want to use c++ strings <string>
string str;
cin >> str; //like that. I see you can't here, but you really should learn these when you can, even if the teacher does not want them, as it is the way to go in c++.
mixing C and C++ I/O sometimes leaves the input and output streams in weird states that cause these kinds of issues.
your class should eventually add a destructor to free that memory for the C strings.
did you free all your memory properly, including the strings in the class?
this error is a memory problem, and given what you are doing, likely one of these:
- failed to free some memory
- tried to release same memory twice
- wrote past end of buffer in one of your strings (string > 30 chars + ending zero)
- etc.
I finally got rid of HEAP CORRUPTION error (I didnt type destructor here but I have it in my code), but I made stupid mistake when allocating memory for on of other dynamic variables I have.
I used () instead of [] and was so focused on why I'm gettingEnter Student name: Enter Student surname: together that I thinked this must be correlated to that, but it was just stupid typing mistake.
Now all work correctly
@seeplus
I tried your code and it work just fine, but for class purposes I need it in 3 files and as I mention above this is just part of the program, and there I again have this problem where Enter Student name: Enter Student surname: are together when using getline, but when using just std::cin>>name; std::cin>>surname; I dont have that. Anyway thanks for help and I hope to never again use char* after this semester.
where Enter Student name: Enter Student surname: are together when using getline
Then you haven't added the cin::ignore() as suggested. Also >> with a string will only extract from the input only to the first white space.
My code as above doesn't have this issue.
Enter Student name: foo
Enter Student surname: bar
Enter Student ID: 123
ID: 123
Name: foo
Surname : bar
Enter Student name: bar
Enter Student surname: foo
Enter Student ID: 234
ID: 234
Name: bar
Surname : foo
If you are still having the issue, I suggest you post you complete current code.