Reading files from a directory

I've written a program which takes one file after the other as input and process them and send the result to an output file.
But now I'm getting weird situation
where my for loop is either executing the loop from k=1 to k=63 (producing output from k=1 to 63)
or from k=64 to k=122 (producing output from k=64 to 122)
but not from k=1 to k=122 directly.

If I try to run the program for k=1 to k=122 then it produces output upto k=63 iteration and does nothing.

The for loop that causes this problem is in main block.
Here's my code



#include<bits/stdc++.h>
using namespace std;


int solve()
{

int total=0;
int noOfBalls=0;
int wicket=0;
string input,country1,country2,battingFirst;
int scoreOfBall;
string Extras,Type,variable,Total;
int f=1,l=1;
for(;(cin>>input) && f;)
{
if(input=="match_type:") {
cin>>Type;
if(Type=="ODI") {f=1;}
else {f=0;cout<<endl;}
}
if(input=="teams:")
{
scanf("-%s-%s",country1,country2);
}
if(input=="team:")
{
f=0;
cin>>battingFirst;
cout<<battingFirst<<"\t\t";
goto a1;
}
}


a1:
for(;(noOfBalls<300) && (wicket <10);)
{

cin>>variable;

if(variable=="extras:")
{

cin>>Extras;
if(Extras=="noballs:") {noOfBalls--;}
else if(Extras=="wides:") {noOfBalls--;}

}
else if(variable=="total:")
{
noOfBalls++;
cin>>Total;
int scoreOfBall=0;
stringstream conv(Total);
conv>>scoreOfBall;
total+=scoreOfBall;
}
else if(variable=="wicket:")
{
wicket++;
}
}
cout<<total<<"\t"<<wicket<<"\t"<<noOfBalls<<endl;

fclose(stdin);

return 1;

}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("001.txt", "w", stdout);
cout<<"S.NO\tTeam\t\tTOTAL\tWickets\tBalls"<<endl;
for(int k=1;k<=122;++k)
{

cout<<k<<"\t";
stringstream ss;
ss << k;
string s=ss.str();
freopen((s + ".yaml").c_str(),"r",stdin);
int g= solve();
}
return 0;
}
Last edited on
I have no idea.

A couple of things.

First, please use code tags. On an initial post you have to type them in manually ([ code] and [/ code], but without the spaces). Then paste your code between the tags. Indentation will be preserved when the code is within code tags.

For subsequent posts and if you edit your post, simply click the "<>" format button to generate the tags. You can edit your post now, highlight your code, and click the "<>" format button. (Indentation may or may not still be preserved at this time. We'll see.)

Second, why are you reopening stdin? Why not just open a std::ifstream and pass it as an argument to solve? You can change solve to take a std::istream& as an argument, and then you can use it with an ifstream, an istringstream or std::cin without any modification to other code. Also, there may be interactions when mixing your C++ streams (cin) with C files (freopen, stdin). I would just stay with C++ for everything.
Last edited on
Topic archived. No new replies allowed.