So My input document has the following data

56 38
A
7 8

My program:

#include <iostream>
#include <fstream>


using namespace std;


int main()
{
ifstream inData;
ofstream outData;
int num1, num2, num3, num4;
char ch1;


inData.open("c:\\inDatat.txt");
outData.open("c:\\outData.txt");


inData >> num1 >> num2;
outData << "Sum of " << num1 << " and " << num2 << " = " << num1 + num2 << endl;


inData >> ch1;
outData << "The character that comes after " << ch1 << " in the ASCII set is B." << endl;


inData >> num3 >> num4;
outData << "The product of " << num3 << " and " << num4 << " = " << num3 * num4 << endl;


inData.close();
outData.close();


return 0;
}

Output on new document should look like this:

Sum of 56 and 38 = 94
The character that comes after A in the ASCII set is B.
The product of 7 and 8 = 56

My output:

Sum of-858993460and-858993460=-1717986920
The character that comes afterÌin the ASCII set is B.
The product of-858993460and-858993460=687194768

Help!

Recommended Answers

All 5 Replies

Me again.

inData.open("c:\\inDatat.txt"); // something wrong here?

dude i compiled and executed ur code... output was just as expected ....

I'd bet his file is named inData.txt, in his code it's inDatat.txt. You probably named it as in the code.

yeah.. that should be it .. since the int's are not initialised either ... i just copied the name from the code and created the file.. OP please check that..

I'd bet his file is named inData.txt, in his code it's inDatat.txt. You probably named it as in the code.

Yep, you've probably nailed it.

Exo - you should always test for successful file opening (particularly input files) before using them. As you see, it's quite possible that the attempted file open operation actually failed. It's as simple as:

inData.open("c:\\inDatat.txt");
if( ! inData )
{
    //do some error handling here
}
else
  //file successfully opened, proceed with the program
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.