[Solved] Why does this program crashes?


No, no, no, just use std::string! Easier to use, and easier to understand!

#include<iostream>
#include <string>
using namespace std;  

int main(){
    string name,add;
    cout<<"Name: ";
    getline(cin, name); // A name probably has more than 1 word
    cout<<"\n\tadd: ";
    cin>>add;
    cout<<"\n\tName:"<<name;
    cout<<"\n\t Add:"<<add;
    return 0;
}

As far as your problem goes with your original code, it is that you haven’t allocated any memory for your char*, and are reading into memory that isn’t yours.

1

solved Why does this program crashes?