0

I have a strange problem. Whenever I add an extra loop to run for T times, it causes a segmentation fault. I'm trying to solve this problem: http://www.spoj.com/problems/ABSYS/ So if I enter the input test cases one by one, it produces the correct output. However when I try to accept input for T number of times, it leads to a segmentation fault. I tried to debug and it says "s cannot access memory at address xyz". Sorry for the poor formatting.

Update: I'm getting a Wrong Answer on SPOJ, and I've checked the sample test cases. Can someone help me in finding out which tricky cases need to be taken care of? I think it's got to do with blank lines! How can I skip blank input lines? http://ideone.com/XUG4kS :(

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <sstream>
using namespace std;

int main()
{
    int Z;
    cin>>Z;

    /* **This condition leads to a segmentation fault, if I remove
       this condition, the code runs fine. I tried using a 
       for-loop to accept input N number of times but that
       didn't work either. So if I remove this Z loop, 
       my program works**! */   
    while(Z--) 
    {
        string s;
        getline(cin, s);
        istringstream iss(s);
        vector<string> numbers;

        do
        {
            string sub;
            iss >> sub;
            numbers.push_back(sub);
        } while (iss);

        // cout<<numbers[0]<<endl;
        // cout<<numbers[2]<<endl;;
        //cout<<numbers[4]<<endl;
        string a = numbers[0];
        string b = numbers[2];
        string result = numbers[4];
        int expression=0; int i=0;

        while(a[i]!='\0')
        {
            if(a[i]=='m')
                { expression=1;  break;}
            i++;
        }

        // cout<<i;
        // a[i]='x';
        // a.erase(i+1, i+7);
        // cout<<a;
        i=0;

        while(b[i]!='\0')
        {
            if(b[i]=='m')
                {expression=2;  break;}
            i++;
        }

        i=0;

        while(result[i]!='\0')
        {
            if(result[i]=='m')
                {expression=3;  break;}
            i++;
        }
        // cout<<expression<<endl;

        switch( expression )
        {
            int temp1, temp2;
            case 1:
                temp1 = atoi(result.c_str());
                temp2 = atoi(b.c_str());
                cout<<temp1-temp2<<" + "<<b<<" = "<<result<<endl;
                break;

            case 2:
                temp1 = atoi(result.c_str());
                temp2 = atoi(a.c_str());
                cout<<a<<" + "<<temp1-temp2<<" = "<<result<<endl;
                break;

            case 3:
                temp1 = atoi(a.c_str());
                temp2 = atoi(b.c_str());
                cout<<a<<" + "<<b<<" = "<<temp1+temp2<<endl;
                break;

        }
    }

    return 0;
}
user3125772
  • 73
  • 10
  • Technically, `while (Z--)` leads to UB because when `Z` is 0 going in, it will be decremented and underflow (which is UB). Also, your loop to parse the string assumes that `sub` was read without any errors. Try entering something like `123 456 789 ` (note the trailing space) into stdin. You should end up with an extra blank string in the vector. – chris Jun 12 '14 at 16:15
  • But it worked for all the problems that I solved so far! – user3125772 Jun 12 '14 at 16:15
  • I tried this " for(int f=1; f<=Z; f++)" but it also resulted into a seg-fault – user3125772 Jun 12 '14 at 16:17
  • 2
    Well, on the same topic, you didn't check that `Z` was read correctly either. If this is pre-C++11, `Z` could be anything if that read fails. Also, that `cin>>Z;` is going to [mess up](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) your first `getline(cin, s);`. – chris Jun 12 '14 at 16:18
  • @chris How would decrementing `Z` from 0 result in underflow for a signed integral type? – Mark B Jun 12 '14 at 16:22
  • Have you tried the following: `while (Z > 0) { ... ; --Z }` ? Just writing this loop condition a bit more clearly may help to see what the problem is in the debugger. As chris said above, you need to validate your `cin` input to make sure what you're getting is what you expect. – djikay Jun 12 '14 at 16:24
  • @MarkB, Oh wow, my bad. That was absolutely duh-worthy. Sorry for that one, user3125772. – chris Jun 12 '14 at 16:26
  • 1
    @user3125772 - `But it worked for all the problems that I solved so far!` You don't write programs this way. Each program will need different analysis and coding. Unless you've written a general function, a utility class, etc., you don't use code "because it worked for another problem" just willy-nilly. – PaulMcKenzie Jun 12 '14 at 16:38
  • Okay it worked, but again for second/third input it's ignoring the first input digit. – user3125772 Jun 12 '14 at 17:31
  • Still, *Wrong Answer*, correct output for sample cases though: ? http://ideone.com/XUG4kS :( – user3125772 Jun 12 '14 at 19:23
  • @chris can you help me with the test cases? – user3125772 Jun 14 '14 at 16:27

0 Answers0