Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I was solving a problem on codeforces and it gave me memory exceeded and even on my local compiler it says 'std::bad_alloc'
can someone explain why and how to solve this !!!
int tst;
cin >> tst;
while(tst--){
string s;
cin >> s;
ll n=s.length();
string ans;
if(n==2){
cout<<s<<endl;
}
else{
if(n%2!=0){
for(int i=0;i<n;i+2){
ans.push_back(s[i]);
}
ans.push_back(s[n-1]);
}
else{
for(int i=0;i<n;i+2){
ans.push_back(s[i]);
}
}
cout<<ans<<endl;
}
}
So, firstly, specify your problem statement.
Secondly, please, pay attention to this cycle:
for(int i=0;i<n;i+2){
ans.push_back(s[i]);
}
The cycle which is written in this way would be infinite, because You are not incrementing i at all, and i will be equal to zero all way cycle goes(infinite times).
So, additionally, You are trying to enlarge Your container by n element in an infinite cycle. You may see, memory is not an infinite resource.
To fix this issue, just rewrite cycle like that:
for(int i=0;i<n;i+=2){
ans.push_back(s[i]);
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I'm writing a program to generate a line equation using given slope and y-intercept, below are the codes i extracted from my program:
int m,c;
cout<<"m >>";
cin>>m;
cout<<endl;
cout<<"c >>";
cin>>c;
cout<<endl;
if (m=0){
cout<<"y= "<<c;
}
else if(m>0){
if (c>0){
cout<<"y="<<m<<"x+"<<c;
}
else if (c<0){
cout<<"y="<<m<<"x"<<c;
}
else {
cout<<"y="<<m;
}
}
else {
if (c>0){
cout<<"y="<<m<<"x+"<<c;
}
else if (c<0){
cout<<"y="<<m<<"x"<<c;
}
else {
cout<<"y="<<m;
}
}
When I input 8 8, the output is as follow:
y=0x+8
When I input 8 -8, the output is as follow:
y=0x-8
When I input 0 5, the output is as follow:
y=0x+5
Even when I try adjust the front part of my codes to
cin>>m>>c;
It doesnt seem helping, which part of my codes has given such error ?
You are setting m to 0 in the first if check. Should be using == not =.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I can't find a problem with my code. It's loading in DEV C++ but after that the window with "Program has stopped working" pop up.
fstream file;
file.open("dane1.txt");
string linia;
string tab[5];
int i = 0;
do
{
getline(file,linia);
cout<<linia<<endl;
tab[i]=linia;
i++;
}
while(!file.eof());
file.close();
ofstream file2("wynik.txt");
if (file2)
{
for(int i=5;i>0;i--)
{
file2<< tab[i];
file2<< endl;
}
}
else
{
cout<<"You have problem with file!"<<endl;
}
pliki.close();
I want to get lines from 1st file (dane1.txt) and then put it in diffrent order in fail "wyniki.txt"
string tab[5];
// ...
for(int i=5;i>0;i--)
{
file2<< tab[i];
file2<< endl;
}
The first iteration of this for loop attempts to access tab[5] which, of course, doesn't exist since the five-element tab array contains only tab[0] through tab[4]. Undefined behavior, and the near-certain reason for your program crashing.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this simple program to formulate a table of any given number however at the end i want the user to be prompted to either end the program or formulate another table.however the loop does not occur.(i'm only a newbie)
int table(){
int tablenumber;
int tablecount;
cout<<"which number's table would you like to print?"<<endl;
cin>>tablenumber;
cout<<"till which number would you like to multiply it?"<<endl;
cin>>tablecount;
for(int i=0; i<=tablecount; i++){
cout<<tablenumber<<" X "<<i<<"="<<tablenumber*i<<endl;
}
}
int main(){
bool yes=true;
bool no=false;
char answer= yes;
while(answer==true){
table();
cout<<"would you like to formulate another table?(yes/no)"<<endl;
cin>>answer;
}
return 0;
}
The problem is that answer is a char and you are trying to compare it with a bool. true and false are always zero (false) and any non zero number (true), so once you read info into answer the ascii value of the inputted char will not be equal to 0 (The int value for false).
Instead read input and loop while answer equals yes (Or y/Y) since answer is a char. Or make answer a string:
string answer = "yes";
while (answer == "yes" || answer == "Yes") {
//code
}
As a followup on #GBlodgett's answer, I'd like to point out a possible solution:
do{
table();
cout<<"would you like to formulate another table?(yes/no)"<<endl;
cin >> answer;
}while(answer == "yes");
As you can see, you don't even need those two bools. However, since you want the answer to be "yes", you need to make answer a string. Good luck!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm trying to store values in double array in a while loop with a terminating value of -1. It seems the values are being stored fine but when -1 is entered the loop does not terminate. Any ideas? Thank you so much!
screen shot of the problem area
int main(void)
{
double a[7],dev[6],mean, std;
int i,n;
int entered;
char letg[6];
cout<<"Please enter the test grades one at a time (max 6)\n";
cout<<"enter a -1 when you are done entering scores\n";
//based off class notes
i=0;
cin>>entered;
while (entered>0)
{ a[i]=entered;
i++;
cin>>entered;
}
Change
{while (entered>0)
to
while (entered>0) {
Your process is executing the statement a[i]=entered; indefinitely. You should put the braces after the while loop:
while (entered>0)
{
a[i]=entered;
i++;
cin>>entered;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'M trying to make a code that is able to increase int 'd' by one each time the a+b reach 20.
and if there still any number less than 20 then this will be the int 'c'.
but instead of getting the right result in my next program which is
49-0
i get this wrong answer
47-40
what should i do ?
#include <iostream>
using namespace std;
int main(){
int a=50;
int b=18;
int c=a+b;
int d=0;
int i;
for(i=0;i<c;i++)
{
while(c>20)
{
d+=1;
c=c-20;
break;
}}
cout<<d<<"-"<<c;
return 0;
}
The problem is in your while loop:
while(c > 20)
{
d+=1;
c=c-20;
break;
}
The loop will only execute once because of your break statement.