C++ runtme error


  • 0
    L

    My C++ solution returns the correct answer when I run the code. But when I submit I get a runtime error. I have read the FAQs but can't figure out why it won't let me submit

    //break up 'secret' string variable into individual characters, store in array
            string S[4];
            for (int i = 0; i < 4; i++)
        	{
    		    S[i] = secret.substr(i,1);
    	    }
    	    
    	    //do the same for 'guess'
    	    string G[4];
            for (int j = 0; j < 4; j++)
        	{
    		    G[j] = guess.substr(j,1);
    	    }
    	    
    	    //test for bulls, add one bull if there is a match
    	    int bulls = 0;
    	    for (int k = 0; k < 4; k++)
        	{
    		    if (G[k] == S[k])
    		    bulls++;
    	    }
    	    
    	    //test for cows, add one if match
    	    int cows = 0;
    	    for (int l = 0; l < 4; l++)
        	{
    		    if ((G[l] == S[0])||(G[l] == S[1])||(G[l] == S[2])||(G[l] == S[3]))
    		    {
    		        cows++;
    		    }
    	    }
            cows = cows - bulls; //so bulls aren't double counted
            
            //convert bulls and cows integers into strings
            stringstream ss;
    	    ss << bulls;
    	    string bull = ss.str();
    	    
    	    stringstream cc;
    	    cc << cows;
    	    string cow = cc.str();
    	    
            //display result, return string called hint
            string hint;
            hint = bull + "A" + cow + "B";
            cout << hint;
            return hint;

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.