HELP

C

coinjo

#include<fstream>
using namespace std;

int main()
{
ifstream indata;
ofstream outdata;

indata.open("D:\\C++\\New Folder\\infile.txt");
outdata.open("D:\\C++\\New Folder\\outfile.txt");

int a=0;
int i=0;
int b=0;

while(a<9)
{
indata>>i;
b=i;
outdata<<b<<" ";
a=a+1;
}
indata.close();
outdata.close();

return 0;
}

This is in the infile.txt

1
2
3
4
5
6
7
8
9

The infile.txt and the outfile.txt are both in the same folder but when
i run the program even after deleting the outfile.txt it shows this:
0 0 0 0 0 0 0 0 0

rather than this:
1 2 3 4 5 6 7 8 9

PLEASE HELP!
 
R

Ron Natalie

coinjo said:
#include<fstream>
using namespace std;

int main()
{
ifstream indata;
ofstream outdata;

indata.open("D:\\C++\\New Folder\\infile.txt");

You don't test this fof success.
outdata.open("D:\\C++\\New Folder\\outfile.txt");

int a=0;
int i=0;
int b=0;
Why are these declared here and only used inside the block?
while(a<9)

for(a = 0; a< 9; ++a)
is a cleaner idiom.
{
indata>>i;

You don't test to see if this succeeds.

What's the point of this assigment, why not just output i.
outdata<<b<<" ";
a=a+1;
}
indata.close();
outdata.close();

I'd check to returns of the open and the >> operations and
put some debug prints in. I suspect i never has a value
other than the zero it was initialized with.
 
D

Dave Townsend

coinjo said:
#include<fstream>
using namespace std;

int main()
{
ifstream indata;
ofstream outdata;

indata.open("D:\\C++\\New Folder\\infile.txt");
outdata.open("D:\\C++\\New Folder\\outfile.txt");

int a=0;
int i=0;
int b=0;

while(a<9)
{
indata>>i;
b=i;
outdata<<b<<" ";
a=a+1;
}
indata.close();
outdata.close();

return 0;
}

This is in the infile.txt

1
2
3
4
5
6
7
8
9

The infile.txt and the outfile.txt are both in the same folder but when
i run the program even after deleting the outfile.txt it shows this:
0 0 0 0 0 0 0 0 0

rather than this:
1 2 3 4 5 6 7 8 9

PLEASE HELP!

Aggghhhh !!

This is horrible!!

This is supposed to be C++ !! Make MetallicDisk a real class, not just a
struct to hold some numbers.
MetallicDisk should have the responsiblities of calculating the volume and
attaching another disk.

class MetallicDisk
{
public:
// constructors....
MetallicDisk( int Inner, int Outer, int Height );
MetallicDisk( const MetallicDisk& otherDisk);

double Volume ( ){ return _height * PI * ( _outer*_outer -
_inner*_inner ) ; }

MetallicDisk Attach( const MetallicDisk& Other )
{
// put the logic in here to determine how the disks can be
attached & return
// the resultant disk....
if ( _inner == Other._outer etc etc....
{
// compute new radii, heights...
}

// create new object and return it...
return MetallicDisk( NewInner, NewOuter, NewHeight );
}
private:
int _inner;
int _outer;
int _height
};


MetallicDisk Attach( const MetallicDisk& Disk1, const MetallicDisk& Disk2)
{
MetallicDisk Result( Disk1.Attach(Disk2) );

double v = Result.Volume();

cout << "blah blah blah, the new disk has volume " << v << "\n";


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,902
Members
47,443
Latest member
RobertHaddy

Latest Threads

Top