C++ ideone.com上的SIGXFSZ?
我想按升序打印字符串中的重复字符。此代码编译但给出了<代码> SigxFsz < /Cuffe运行时错误,没有在线C++编译器输出…有什么建议吗C++ ideone.com上的SIGXFSZ?,c++,C++,我想按升序打印字符串中的重复字符。此代码编译但给出了 SigxFsz < /Cuffe运行时错误,没有在线C++编译器输出…有什么建议吗 #include <iostream> #include <string.h> #include <ctype.h> using namespace std; int main() { unsigned int i; char str[26]; int a[26]; for(i=0;i<26;i++
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
int main()
{
unsigned int i;
char str[26];
int a[26];
for(i=0;i<26;i++)
a[i]=0;
A:
i=0;
cout<<"Enter the string:";
cin>>str[i];
while(str[i]!=13)
{
if(isalpha(str[i]))
i++;
else{
cout<<"Invalid string";
goto A;
}
}
cout<<"You Enterd:"<<str;
for(i=0;i<strlen(str);i++)
++a[str[i]-97];
cout<<"\nLetters Frequency:";
for(i=0;i<26;i++)
cout<<a[i]<<" ";
cout<<"\nDuplicates in sorted order:";
for(i=0;i<26;i++)
{
if(a[i]>1)
cout<<char(i+97);
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
无符号整数i;
char-str[26];
INTA[26];
对于(i=0;i问题1
问题1
如果您不以有效的输入结束输入,您将在ideone上获得一个SIGXFSZ
,因为您将尝试读取比cin
上更多的数据
除此之外,我还修复了几个其他错误:首先,Eric Z指出的cin>>str[I]
。此外,你的while(str[I]!=13)
应该是while(str[I]!=0)
如果不以有效输入结束输入,您将在ideone上获得一个SIGXFSZ
,因为您将尝试读取比cin
上可用的数据更多的数据
除此之外,我还修复了几个其他错误:首先,Eric Z指出的cin>>str[I]
。此外,你的while(str[I]!=13)
应该是while(str[I]!=0)
请下次格式化您的代码。说明就在您用来输入问题的文本框旁边。转到A;
-Gasp!通常是“在线”C++编译器不支持从<代码> CIN <代码>阅读。你在自己的计算机上尝试过吗?有时候,很少有GOTO比你必须做的更清楚,避免它。这不是一个时间。不要使用Goto!!!请在下一次格式化你的代码。在你输入问题的文本框旁边有指令。ODE > Goto A; -GASP!通常“在线”C++编译器不支持从<代码> CIN < /代码>中阅读。你在自己的计算机上尝试过吗?有时候,Goto比你必须做的更清楚,避免它。这不是其中的一个时间。不要使用Goto!!!!
char str[26];
//..
cout<<"Enter the string:";
// should be cin >> str if you want to input a string,
// cin >> str[i] is used to input a single character only.
cin>>str[i];
// should be while(str[i]!='\0')
// because a C-style string is terminated with '\0'.
while(str[i]!=13)
{
if(isalpha(str[i]))
i++;
else{
cout<<"Invalid string";
goto A;
}
}
bool isInvalid = true;
while (isInvalid)
{
// read input
// while-loop validating the input
if (str[i] == '\0') isInvalid = false;
}