so i have this assignment that i've completed most of it, i just can't figure out how to make a call to a function. And i'm having problems writting to a file. PLEASE help.
The specifications and requirements for my program are:
The input to your program is a text file: "c:\calculate.txt". You will create your own file for testing your program. For grading, we will test the program using our file. So make sure your file tests all of the operations, using all possible input variations.
The input operator can be upper or lower case.
The input values of x and y can be either int or double, and are stored in the calculator as double.
The program outputs to the screen the result of each expression evaluation on a separate line.
You must write 4 separate functions: one for each operation.
The input file may contain zero or more lines (your code needs to handle empty files).
C++ programing?
EDIT:
If you want, you can email me your code and I can take a quick look.
EDIT:
Your problem is that your main function doesnt know about the variable answer. The main and all your function have seperate variables, and they dont know about each other.
Before that cout put this.
double answer = function(x, y);
If this isnt your actual problem, what is the actual error message you get.
===========================
To write to a file
#include %26lt;fstream%26gt;
ofstream outfile;
outfile.open("newfile");
outfile %26lt;%26lt; 1; // 1 is now in the file, used the same way as cout
outfile.close(); // to close the file
To call a function:
int myFunc(int); // before main
int main()
{
myFunc(10); // to call
}
int myFunc(int myArg) // function
{
return myArg*2;
}
Let me know if you have more specific questions. Hope that helps :)
Reply:OK well I can't imagine how you could have "most of it done" without knowing how to "call a function" but I'll assume you have most of it done anyway.....
So you parse the input file and there is a line that is not an empty line and you parse the three space delimited "tokens" (after making them all upper case) and now you have some "char arrays" with X, Operation and Y.
You have already called some functions to read to files and some to convert the X and Y into doubles and then you have a "switch( Operation )" statement with a "case 'D':" type thingy in it that figures out that you have the following line of input: "x d y if y %26gt; 0, returns x / y, else returns -1".
Of course, you have a function called something like "double MyStupidDivide( double X, double Y )" that you have created that does something like { return X %26gt; Y ? X / Y : -1; }
" and so naturally you call it like this: "double MyStupidResult = MyStupidDivide( x, y );" and thats about it.
It's easy.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment