Home > Back-end >  Please god help take a look at the c program, why doesn't run!
Please god help take a look at the c program, why doesn't run!

Time:10-09

#include
#include
using namespace std;

The class Mystring
{
Int m, n;
Char * s1.
Char * s2.
Public:
Mystring ();
Void Strcpy ();
Void Strcmp ();
Void Strlen ();
Void Strcat ();
Void Out1 ();
Void Out2 ();
Void Out3 ();
};

Mystring: : Mystring ()
{
Strcpy (s1, "pear");
Strcpy (s2, "apple");
}

Void Mystring: : Strcpy ()
{
Strcpy (s1, s2);
}

Void Mystring: : Strcmp ()
{
M=STRCMP (s1, s2);
}

Void Mystring: : Strlen ()
{
N=strlen (s1);
}

Void Mystring: : Strcat ()
{
Strcat (s1, s2);
}

Void Mystring: : Out1 ()
{
Cout}

Void Mystring: : Out2 ()
{
Cout}

Void Mystring: : Out3 ()
{
Cout}

Int main ()
{
int a;
Mystring p;
Cout<& lt;" S1=the pear, s2=apple \ n1 copy (2) to compare 3. Statistical number 4. Connection "& lt; Cin> a;
If (a==1)
{
P. trcpy ();
P.O ut1 ();
}
Else if (a==2)
{
P. TRCMP ();
P.O ut2 ();
}
Else if (a==3)
{
P. trlen ();
P.O ut3 ();
}
Else if (a==4)
{
P. trcat ();
P.O ut1 ();
}
The else
Cout<& lt;" Wrong!" return 0;
}

CodePudding user response:

Don't look
Haven't seen in the constructor to s1, s2 allocates memory copy, must be wrong,

CodePudding user response:

First to s1 and s2 allocate space, size, custom,
Char * s1=new char [20].
Char * s2=new char [20].
Second, under vs2017 use strcpy and strcat unsafe, it would suggest that you use the
Strcpy_s (s1, 20, "pear");
20 strcat_s (s1, s2);

CodePudding user response:

Pointer variables are assigned address first (also can give a value), otherwise it's easy to randomly assigned to the system workspace, this probability is very big still

CodePudding user response:

Give a debugging is bad
  • Related