SIGXFSZ on ideone.com? [closed]
I want to print repeated characters in a string in ascending order. This code compiles but gives a SIGXFSZ
runtime error with no output on online c++ compilers... any suggestions?
#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;
}
You'll get a SIGXFSZ
on ideone if you don't end your input with valid input, because you will try to read more data than is available on cin
. Here's a working run.
Aside from that, I fixed a couple other bugs: First, the cin >> str[i]
that Eric Z pointed out. Additionally, your while(str[i]!=13)
should be while(str[i]!=0)
.
Problem 1
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];
Problem 2
// 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;
}
}
Problem 3
Don't get used to goto
. It's not considered as a good programming style in most cases, as it make your code unnecessarily complicated, hard to read.
You can use the following loop to replace goto
.
bool isInvalid = true;
while (isInvalid)
{
// read input
// while-loop validating the input
if (str[i] == '\0') isInvalid = false;
}
精彩评论