Thread: [Dev-C++] HELP---Read/Write Files in Dev C++
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Julian E. <j_e...@ya...> - 2001-09-24 21:27:43
|
I have a program that complies and runs in both Microsoft C++ 6.0, and my schools Dec Unix C++ complier. I opened a new project in Dev C++ make it a console application. I added these three files to my project pwrpro.h, pwr.cpp, runpwr.cpp The program compiles with no errors. The problem is that this program should read data from a file and the then write a new file, with calculations in it. It writes a blank file. I am wondering if I am missing something for it to work correctly in this IDE. When i do not put my file in.data in the directory it correctly prints "the file is not found" in the output file. ANY HELP WILL BE GREATLY APPRECIATED!!!! //---------- // Specification File (pwrpro.h) // This module provides exponentiation functions. //---------- int PowerOfInt ( int someInt, int exp ); // PRE: Assign int (someInt) and exp > 0 // POST: Firstnum raised to the power "expon1" //(NOTE: Large exponents may produce overflow) float PowerOfFloat ( float someFloat, int exp ); // PRE: Assign float (someFloat) and assigned some exp > 0 // POST: someFloat raised to the power "exp" //(NOTE: Large exponents may produce overflow) bool NonZeroInt (int exp); //PRE: Take infile exp //POST: make sure exp > 0 ----------------------------------------------- ----------------------------------------------- ----------------------------------------------- //---------- // Specification File (pwr.cxx) // This module provides exponentiation functions. //---------- #include <iomanip.h> #include "pwrpro.h" int PowerOfInt ( int someInt, int exp ) //........... // PRE: Assign int (someInt) and exp > 0 // POST: Firstnum raised to the power "expon1" //(NOTE: Large exponents may produce overflow) //........... { int i; int partialVal = 1; for ( i = 1; i <= exp; i++) partialVal *= someInt; return partialVal; } float PowerOfFloat (float someFloat, int exp ) //........... // PRE: Assign float (someFloat) and assigned some exp > 0 // POST: someFloat raised to the power "exp" //(NOTE: Large exponents may produce overflow) //........... { int i; float partialVal = 1.0; for ( i = 1; i <= exp; i++) partialVal *= someFloat; return partialVal; } bool NonZeroInt (int exp) //.......... //PRE: Take infile exp //POST: make sure exp > 0 //.......... { bool value; if (exp <= 0) value = false; else value = true; return value; } ------------------------------------------------- ------------------------------------------------- ------------------------------------------------- //************************************************************************* //PURPOSE // runpwr.cxx) // This program reads IntNum, IntExp, FloatNum, FloatExp from an input file and calculates // IntNum^Int Exp and Float Num^ Float Exp. // Note X^n denotes X raised to the nth power. //************************************************************************* #include <fstream.h> #include <iomanip.h> #include "pwrpro.h" int main() { ifstream inFile; ofstream outFile; inFile.open("in.data", ios::nocreate ); outFile.open("out.data"); if (! inFile) { outFile<<" ERROR: Unable to locate file named 'in.data' "<<endl; return 0; } int IntExp, FloatExp, IntNum; float FloatNum; inFile >> IntNum >> IntExp >> FloatNum >> FloatExp; while (! inFile.eof() ) { if (NonZeroInt (IntExp)== false) outFile <<"This Function accepts only " <<"postive integer exponents!" <<" Your exponent is zero or negative. "; else { outFile <<IntNum << "^"<< IntExp << " = " << setw (10) << PowerOfInt(IntNum,IntExp) <<endl; } if (NonZeroInt (FloatExp)== false) outFile <<"This Function accepts only " <<"postive integer exponents!" <<" Your exponent is zero or negative. "; else { outFile <<FloatNum << "^"<<FloatExp << " = " << setw (10) <<setiosflags(ios::fixed) << setprecision (2)<<PowerOfFloat(FloatNum, FloatExp) <<endl <<endl; } inFile >> IntNum >> IntExp >> FloatNum >> FloatExp; } inFile.close(); outFile.close(); return 0; } __________________________________________________ Do You Yahoo!? Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger. http://im.yahoo.com |
From: Luigi S. <gi...@li...> - 2001-09-25 11:09:13
|
Hi! I've tried the code that you posted (I broke it into 3 files then used Dev-C++ to make a project, compile the project and run it) with the following as "in.data": in.data --------- 3 2 2.5 2 3 3 2.5 3 -- It produced the following output: out.data ------------ 3^2 = 9 2.5^2 = 6.25 3^3 = 27 2.50^3 = 15.62 -- So it looks Ok to me. At 14.27 24/09/01 -0700, you wrote: >I have a program that complies and runs in both >Microsoft C++ 6.0, and my schools Dec Unix C++ >complier. I opened a new project in Dev C++ make it a >console application. I added these three files to my >project pwrpro.h, pwr.cpp, runpwr.cpp The program >compiles with no errors. The problem is that this >program should read data from a file and the then >write a new file, with calculations in it. It writes a >blank file. I am wondering if I am missing something >for it to work correctly in this IDE. When i do not >put my file in.data in the directory it correctly >prints "the file is not found" in the output file. -- gisan gi...@li... |