Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone today i encountered with new error and i dont know the reason. please help me.
when i run the code this error appears :
this application has requested,the runtime to terminate it in an unusual way.

What I have tried:

C++
#include <bits/stdc++.h>
#include <iostream>
#include <cmath>

using namespace std;

// Complete the repeatedString function below.
long repeatedString(string s, long n) {

   long counter=0 ;

for (int i=0 ; i<(n/s.size())+1 ; i++) {

        for (int k=0 ; k<s.size() ; k++) {
            s+=s[k] ;

            if (s.size()==n) {
                break ;
            }
        }

            if (s.size()==n) {
                break ;
}
}

for (int u=0 ; u<s.size() ; u++) {
    if (s[u]=='a') {
        counter++ ;
    }
}

return counter;
}

int main() {

cout <<repeatedString("a",1000000000) ;

return 0;
}
Posted
Updated 27-Oct-19 8:10am
v2
Comments
Afzaal Ahmad Zeeshan 26-Oct-19 18:25pm    
That does not help us understand the problem, please run the program with a debugger.
CPallini 28-Oct-19 5:32am    
What is your code expected to do?

I think you are exceeding the available memory space of the process. Look at the loops. The inner one goes from 0 to s.size(). Within the loop you are appending a part of the string to itself. Every time you do this the size of the string gets bigger until the size equals the value passed. What happens if the size never matches n? The for loop will continue until all available memory is used and then the process will terminate.

Try changing your checks to if( s.size() >= n ).

Also try passing smaller numbers to make sure the logic works as you expect it to. Start with small values like 10 and then gradually increase the number in orders of magnitude and see how long it can run. What I mean is try 10, 100, 1000, 10000, etc...
 
Share this answer
 
What about debugging your code? It is a common task.

Read this tutorial about basic debugging tactics to expand your skills.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900