-1

I was creating a code for rotation of specif number in a string and initially i created it for one user input ! It worked as soon as i added on logic for multiple test case its is showing floating point exception.....

can anyone tell me in the code what caused that error !

#include<iostream>
#include<cstring>
using namespace std;
void stringrot(char str[],int n)
{   

    int l=strlen(str);
    char temp[100];
    int k=n%l;
    for(int i=0;i<k;i++)
    {
        temp[i]=str[(l-k+i)];
    }
    for(int i=0;i<l-k;i++)
    {
        temp[k+i]=str[i];

    }
    temp[l]='\0';
    cout<<temp;


}
int main()
{   
    int test;
    cin>>test;
    while(--test>=0)
    {   
        char str[100];
        cin.getline(str,50);
        int n;
        cin>>n;
        stringrot(str,n);
    }
}

Here is the code !

Prasad Khode
  • 6,602
  • 11
  • 44
  • 59

2 Answers2

1

Let's take a closer look at the while loop in main

while(--test>=0)
{   
    char str[100];
    cin.getline(str,50);
    int n;
    cin>>n;
    stringrot(str,n);
}

Loop Iteration 1

    cin.getline(str,50); << reads a line
    cin>>n;              << reads an integer. After enter is pressed. Enter 
                            is not an integer. Enter is not read and stays 
                            in the stream
    stringrot(str,n);

Loop Iteration 2

    cin.getline(str,50); << reads a line. Fortunately there is an enter
                            still in the stream making it really easy to 
                            find the end of the line. It's the first 
                            character that's going to be read.
    cin>>n;              << reads an integer. Unfortunately the user 
                            probably thinks they are typing in the string 
                            and doesn't type in a number. Oops. 
    stringrot(str,n);

Anyway stringrot gets called with stringrot("",0), so the first few lines of stringrot wind up looking like

int l=strlen("");   << l will be zero because the string is emtpy
char temp[100];
int k=n%l;          << resolves to 0%0 and an integer divided by zero is BOOM!

Solution:

Change the guts of the while loop to something like

char str[100];
cin.getline(str,50);
int n;
cin>>n;
// discard everything else on the line including the end of line character
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
stringrot(str,n);

Why an integer division results in a floating point error, one that isn't a catchable C++ exception by the way, seems to be mostly historical. More here: Why is this a floating point exception?

Recommendation:

Use std::string not char arrays.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Integer division by zero raises a Floating-Point Exception, because of... reasons. Nice dissection ;) – Quentin Jan 24 '17 at 09:33
0
#include<iostream>
#include<cstring>
using namespace std;
//void stringrot(char str[],int n)
void stringrot(string str,int n)
{

    //int l=strlen(str);
    int l=str.size();
    char temp[100];
    int k=n%l;
    for(int i=0;i<k;i++)
    {
        temp[i]=str[(l-k+i)];
    }
    for(int i=0;i<l-k;i++)
    {
        temp[k+i]=str[i];

    }
    temp[l]='\0';
    cout<<temp;


}
int main()
{
    int test;
    cin>>test;
    while(--test>=0)

    {
        //char str[100];
        //cin.getline(str,50);
        string str;
        cin>>str;
        int n;
        cin>>n;
        stringrot(str,n);
    }
}