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

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.

Related

First input number turns 0 after the executing the codes [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 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 =.

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

Set is not removing the duplicates [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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 am having trouble putting a file of words into a set. I can read the file and the words go into the set but the set doesn't discard the repeated words. Here is the snippet of code that I believe is causing the problem.
using namespace std;
while(readText >> line){
set<string> wordSet;
wordSet.insert(line);
for (std::set<std::string>::iterator i = wordSet.begin(); i != wordSet.end(); i++)
{
cout << *i << " ";
}
}
the sample file is this
1
2
2
3
4
5
5
and the output is exactly the same
As stated in comments, you are not using the std::set correctly. You need to move it, and the for loop, outside of your while loop:
using namespace std;
set<string> wordSet;
while(readText >> line) {
wordSet.insert(line);
}
for (set<string>::iterator i = wordSet.begin(); i != wordSet.end(); i++) {
cout << *i << " ";
}

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

Why do i get this wrong result? [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'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.