Home > Back-end >  Why the results of this program out wrong, ask the great spirit guide
Why the results of this program out wrong, ask the great spirit guide

Time:09-26

#include
#include
Int main ()
{
String a;
Char b;
Getline (cin, a);
Endl;
cout<" Please enter the letter ";
Cin> & gt; b;
Int n, m=0;
for(int i=0; A [I]!='\ 0'; I++) {
M++;
If (a [I]==b) {
n++;
}
}
coutreturn 0;
}

CodePudding user response:

N't initialize, int n =0, m=0;

CodePudding user response:

#include
#include
using namespace std;

Int main ()
{
String a;
Char b;
cout<" Please input string: "
Getline (cin, a);
cout<" Please enter the letters: ";
Cin> & gt; b;
Int n=0, m=0;
for(int i=0; A [I]!='\ 0'; I++)
{
M++;
If (a [I]==b)
{
n++;
}
}
coutreturn 0;
}


Above, changed well, first of all, there is no added using namespace STD.
Then on the eighth line directly endl, no cout (output), endl is line breaks
There is no initialization mentioned above

CodePudding user response:

I guess this program's function is to find the length of the input string, and find the position in the string input letters,
Main problem as the two main answer say:
1 using namespace STD.
2 n to initialise, here according to the need, n=1
One more thing, want to consider abnormal input (input letters not string)
I have modified on the basis of your code, contrast to look at, for reference only.

 # include & lt; Iostream> 
#include

using namespace std;

Int main ()
{
String a;
Char b;
Getline (cin, a);
Cout & lt; <"Please enter the letter";
Cin & gt; & gt; b;
Int n=1, m=0;
For (int I=0; A [I]!='\ 0'; I++) {
M++;
If (a [I]==b)
Cout & lt; The else
n++;
}

If (n & gt; M)
Cout & lt; <"Letters" & lt; The else
Cout & lt;
return 0;
}
  • Related