Why do i get this wrong result? [closed] - c++

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.

Related

Why this code gives run time error on mid calculation? [closed]

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 1 year ago.
Improve this question
int main() {
int start = 0;
int end = 2;
int ret1 = 0;
int mid = start + (end-start)/2;
ret1 = mid;
return ret1;
}
Why is this code giving runtime error?
https://ideone.com/iAdQO0
A nonzero return code from main() indicates an error to many operating systems.
Because any return from main that is not 0 indicates a runtime error.

Memory limit exceeded bad_alloc [closed]

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]);
}

How to fix input and output problem with files in C++? [closed]

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.

Unterminating while loop in c++ [closed]

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;
}

How t solve "Variable 'std::ifstream myfile' has initializer but incomplete type" [closed]

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 am about to make a program that will get a record of grades of students and it will determine it's position in a frequency distribution table. The records will be coming from a file. Here is my code:
#inlcude<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int const ns=40;
int main()
{
int y,x,i,vl=0,l=0,m=0,h=0,vh=0;
int argr[ns];
ifstream myfile ( "file.txt", ios::in);
if (myfile.is_open())
{
while(getline(myfile,x))
for(i=0;i<ns;i++)
{
argr[i]=x;
if(argr[i]<=20 && argr[i]>=0)
vl=vl+1;
else if(argr[i]<=40 && argr[i]>=21)
l=l+1;
else if(argr[i]<=60 && argr[i]>=41)
m=m+1;
else if(argr[i]<=80 && argr[i]>=61)
h=h+1;
else if(argr[i]<100 && argr[i]>=81)
vh=vh+1;
cout<<"Range\t\tFrequency\n\n";
cout<<"0-20\t\t "<<vl<<endl;
cout<<"21-40\t\t "<<l<<endl;
cout<<"41-60\t\t "<<m<<endl;
cout<<"61-80\t\t "<<h<<endl;
cout<<"81-100\t\t "<<vh<<endl;
}
myfile.close();
}
else cout<<"Can't find the file";
return 0;
}
Another problem showed saying, "Invalud preprocessing directive#include"
What should I do?
#inlcude<fstream>
change this to
#include<fstream>
you just got a typo but what kind of terrible IDE are you using that does not show you this immediately?