hello,

i recently joined the USACO's training gateway http://ace.delos.com/usacogate
I made the first problem(http://ace.delos.com/usacoprob2?a=n4y3olVXOJ9&S=ride) and it ran successfully in my dev-cpp compiler. but the online grader threw some runtime error. it was like

Compiling...
Compile: OK

Executing...
> Run 1: Execution error: Your program (`ride') exited with signal
#6 (abort()). The program ran for 0.000 CPU seconds before the
signal. It used 2928 KB of memory.

------ Data for Run 1 ------
COMETQ
HVNGAT
----------------------------

Your program printed data to stderr. Here is the data:
-------------------
terminate_called_after_throwing_an_instance_of_'std::ios_base::failure'
__what():__basic_ios::clear
-------------------
Test 1: SIGNAL 6 (0.000 secs, 2928 KB)

debugging using cerr, i came to a conclusion that there is something with the "fin >>"
statement that is causing problem. my code is

/*
ID: nimeshg1
PROG: ride
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
 using namespace std;
 int main()
 {
     ofstream fout;
     fout.open("ride.out");
     ifstream fin ("ride.in");
     char comets[6], groupers[6];
     int cno=1,gno=1;     
     fin >> comets;
     fin >> groupers;
     for(int i=0;i<6;i++)
     {
     cno *= ((int)comets[i] - 64);
     if(comets[i]=='\0')
     i=6;
     }
     for(int i=0;i<6;i++)
     {
     gno *= ((int)groupers[i] - 64);
     if(comets[i]=='\0')
     i=6;
     }
     if(cno%47 == gno%47)
     fout<<"GO"<<endl;
     else
     fout<<"STAY"<<endl;
     return 0;
 }

Any suggestions?

hello,

i recently joined the USACO's training gateway http://ace.delos.com/usacogate
I made the first problem(http://ace.delos.com/usacoprob2?a=n4y3olVXOJ9&S=ride) and it ran successfully in my dev-cpp compiler. but the online grader threw some runtime error. it was like

Compiling...
Compile: OK

Executing...
> Run 1: Execution error: Your program (`ride') exited with signal
#6 (abort()). The program ran for 0.000 CPU seconds before the
signal. It used 2928 KB of memory.

------ Data for Run 1 ------
COMETQ
HVNGAT
----------------------------

Your program printed data to stderr. Here is the data:
-------------------
terminate_called_after_throwing_an_instance_of_'std::ios_base::failure'
__what():__basic_ios::clear
-------------------
Test 1: SIGNAL 6 (0.000 secs, 2928 KB)

debugging using cerr, i came to a conclusion that there is something with the "fin >>"
statement that is causing problem. my code is

/*
ID: nimeshg1
PROG: ride
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
 using namespace std;
 int main()
 {
     ofstream fout;
     fout.open("ride.out");
     ifstream fin ("ride.in");
     char comets[6], groupers[6];
     int cno=1,gno=1;     
     fin >> comets;
     fin >> groupers;
     for(int i=0;i<6;i++)
     {
     cno *= ((int)comets[i] - 64);
     if(comets[i]=='\0')
     i=6;
     }
     for(int i=0;i<6;i++)
     {
     gno *= ((int)groupers[i] - 64);
     if(comets[i]=='\0')
     i=6;
     }
     if(cno%47 == gno%47)
     fout<<"GO"<<endl;
     else
     fout<<"STAY"<<endl;
     return 0;
 }

Any suggestions?

After opening your streams (after ifstream fin(...)), try to add the following.

if(fin.failed() ­|| fout.failed())
{
    std::cerr << "Could not open streams! Exiting!" << std::endl;
    return -1;
}

This will ensure that if the file failed to open or was read only or whatever, then it will stop the program here instead of trying to handle a failed stream.

Hi,
I am facing the same problem, I would be grateful if you could tell me how did you solve it.
Thanks!

If you read the documentation for the istream operator, then you would learn that you need to first call the "width()" function on the input stream to tell it how many characters you can accomodate with the read operation. Otherwise, the width will be zero and the stream will go into a fail mode when reading from it.

So, instead of this:

 fin >> comets;
 fin >> groupers;

It should be this:

 fin.width(6);
 fin >> comets;
 fin.width(6);
 fin >> groupers;

Note that the width() function has to be called again for every operation like this.

For a simpler approach, that doesn't require these tedious calls to "width()", just use the C++ string class instead.

Hey mike_2000_17,

Thanks for the input! I added the width statements but I am still reading garbage values, any idea why?

ofstream fout("ride.out");
ifstream fin("example.txt");
char ufo[7];
char group[7];

fin.width(7);
fin>>ufo;

fin.width(7);
fin>>group;

puts(ufo);
puts(group);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.