Home > Back-end >  For help, code, the result is different with the book?
For help, code, the result is different with the book?

Time:11-28

# include & lt; Iostream>
# include & lt; string>
# include & lt; iomanip>
using namespace std;
Int findInteger (char * p, int * a)
{
Int j, n=0, I, k;
Char \ [100].
for (i=0; P [I]!='\ 0'; I++)
{
J=0;
While (p [I] <='0' & amp; & P [I] <='9')
{
Temp=p [j] [I];
j++;
i++;
}
If (j!=0)
{
* a=atoi (temp);
a++;
N++;
For (k=0; k\ [k]=0;
i--;
}

}
return n;
}


Int main ()
{
Int I, m, a, [100].
Char line [100].
Cout & lt; <"Please input a string:" & lt; Gets_s (line);
M=findInteger (line, a);
Cout & lt; <"String consists of:" & lt; for (i=0; I & lt; m; I++)
Cout & lt; Cout & lt; system("pause");
return 0;
}

I always show the input strings and integers, strings of the communist party of China are: 0 integers,
But the actual is an integer string,

CodePudding user response:

Modified as follows, for your reference:
 # include & lt; Iostream> 
# include & lt; string>
# include & lt; iomanip>
using namespace std;

Int findInteger (char * p, int * a)
{
Int j, n=0, I, k;
Char \ [100].
for (i=0; P [I]!='\ 0'; I++)
{
J=0;
While (p [I] & gt;='0' & amp; & P [I] <='9')//the building here conditions mistyped
{
Temp=p [j] [I];
j++;
i++;
}
If (j!=0)
{
Temp [j]='\ 0';//add a terminator here
* a=atoi (temp);
a++;
N++;
For (k=0; k\ [k]=0;
i--;
}

}
return n;
}

Int main (int arg c, char * argv [])
{
Int I, m, a, [100].
Char line [100].
Cout & lt; <"Please input a string:" & lt; Gets (line);

M=findInteger (line, a);
Cout & lt; <"String consists of:" & lt;
for (i=0; I & lt; M; I++)
Cout & lt; Cout & lt; system("pause");
return 0;
}

CodePudding user response:

 for (k=0; k\ [k]=0; 

These two lines of code redundancy, because above have temp [j]='\ 0'; , so I don't need to be initialized to zero,