[英]cin.getline() not working inside a while loop
Here is my program to find the frequencies of characters in a multi-line string:这是我在多行字符串中查找字符频率的程序:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
using namespace std;
void occ(char *s);
int main()
{
string s;
char p[355],a[80];
int i,j,n,l=0;
char ch;
cout<<"Enter a string:";
gets(p);
while(ch=='y' || ch=='Y')
{
cout<<"Enter string:";
gets(a,80);
strcat(p,a);
cout<<"\nDo you want to enter another string (y/n):";
cin>>ch;
}
occ(p);
return 0;
}
void occ(char s[])
{
int i;
char ch,m[80];
int j,n=0,k,l,c;
m[n]='\0';
l=strlen(s);
for(int i=0; s[i]!='\0'; i++)
{
int flag=0;
ch=s[i];
for(int k=0; m[k]!='\0'; k++)
{
if(m[k]==ch)
flag=1;
}
c=0;j=0;
if(flag!=1)
{
m[n++]=ch;
while(j<l)
{
if(s[j]==ch)
c++;
j++;
}
if(ch==' ')
cout<<"\nFrequency of character (space) is"<<c;
else
cout<<"\nFrequency of character "<<ch<<" is"<<c;
}
}
}
The input of string inside the while loop is not taking not even for single time while循环内的字符串输入甚至一次都没有
you must initialize your ch
char with y
or Y
in order to enter your loop.您必须使用
y
或Y
初始化您的ch
字符才能进入您的循环。 You can simply do that like this:你可以简单地这样做:
char ch='y';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.