okay so im kind of confused now. . . . . i didn't even have a code to show you cause I know its wrong..I've been absent 2 days on my Programming so its really hard to cope up...i tried researching for ifstream.h and ofstream.h....and so far ifstream.h is for reading a file, and ofstream.h is for writing to a file right??

I got the lab exercise that i missed so I could understand clearly but so far, I understood the ofstream.h.100%(maybe) but no idea ifstream.h..(yea, im using TURBO c++ 4.5 compiler..its really old)

So here's the deal:
What if a problem was to make a program that asks the user for input file(yes, so my inputs are in a file..file to display screen..so its reading to a file right?) with inputs of an arithmetic operator keyword and 2 numbers.(in this case I would want to do simple addition, keyword is "sum")...

Then output(in monitor)..

The sum of (whatever the first input is in the file) and (whatever the second input is in the file) is (the sum of the 2 input file)..

there that's the problem..i got confused because i think i'll need both ofstream.h and ifstream.h...right?...can you please give me a simple code with explanation of it....and I swear this is not an assignment...i just want to cope up with our lesson..Thaks alot! ! !

Member Avatar for iamthwee

A quick reminder I use is to use the first letter.

O -fstream = output/writing
I -fstream = input/ reading

That's about it. So if you need to write out to a file you will need ofstream.

Edit** And what AD said below.

i didn't even have a code to show you cause I know its wrong.

That doesn't matter here -- most of the code we see posted here is wrong. All we require is that you make an effort to write your program, we don't require the code be correct.

researching for ifstream.h and ofstream.h....and so far ifstream.h is for reading a file, and ofstream.h is for writing to a file right??

No. There are no such header files. fstream, ifstream and ofstream are all declared in the same header file named fstream.h (current compilers use <fstream> instead of <fstream.h>

If you can you should upgrade your compiler to either Code::Blocks or VC++ 2010 Express, both are free. If you live in India then you may not have that option because your school most likely requires Turbo C.

You are probably right, you might need to use both... luckily c++ provides fstream.h which is a combination of ifstream and ofstream. If you are just outputting to the screen though you can just use the ofstream object cout provided in iostream.

oh i see..well this is what ive done so far...The input file contains sum 9 9...but the output it gives me is not correct..

#include<fstream.h>

void main()
{
    ifstream in;
    in.open("A57M2_Manuela.txt");
 char SUM[4];
 int x,y;
    in>>SUM;
    in>>x;
    in>>y;
 int sum;
    sum=x+y;
    cout<< "The total of "<<x<<" and "<<y<<" is"<<sum;
}

the output gives me random numbers for x, y and sum.....it should've read the input file which is sum 9 9...see im confused..as i said the problem was to make a program that asks the user for input file(yes, so my inputs are in a file..file to display screen..so its reading to a file right?) with inputs of an arithmetic operator keyword and 2 numbers.(in this case I would want to do simple addition, keyword is "sum")...

Then output(in monitor)..

The sum of (whatever the first input is in the file) and (whatever the second input is in the file) is (the sum of the 2 input file)..

Maybe I misunderstand the problem, but if the first two numbers in the file are 9 and 8 then all you have to do is read x and y, then add them todether. There is no need for SUM array. Just delete lines 7 and 9.

You might want to check and see if you are even opening the file. to do that you can use this

if(in.fail())
    cout << "The file could not be open;"

I would put that right after line 6 to make sure the file is open before you try to read from it.

It still won't give me the result...maybe i'll rephrase the my problem...

Lets say: I have a file..that file contains..
sum 9 9...

what I'm going to do is read from that file so that it would give me an output of

The sum of 9 and 9 is 18

gets? in here i think i'll use ifstream...oh my, i hope someone would help me

Try printing out avarything you read in as soon as you read it.
Can you use STRINGs rather than CHARs? If not, make your CHAR array larger -- say [20].

...oh my,

Channelling George Takai, I see... ;-)

Guys, i finally figured out what to do..tell me if this is okay,,,it worked for me so far..

#include<fstream.h>
#include<cstring.h>
#include<math.h>
#include<conio.h>

void main()
{
 int x,y;
 ifstream in;

 in.open("file.txt");
 string kw;
 in>>kw;
 in>>x;
 in>>y;

 if(kw=="sum"||kw=="plus"||kw=="total")
 {
  cout<< "The "<<kw<<" of "<<x<< "and "<<y<< "is "<<x+y;
 }
 else if(kw=="difference"||kw=="minus"||kw=="subtraction")
 {
  cout<< "The "<<kw<<" of "<<x<< "and "<<y<< "is "<<x-y;
 }
 else
 {clrscr();}

 in.close();

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