I'm in COSC 1436-8001 in community college at the moment learning C++.
I'm doing a problem in the book and i'm just confused on the last part f. I've done all of the steps successfully but i'm just confused on how to
"F. Write a C++ program that tests the statements in parts a through e."
I've never been instructed on how to "test" a program, I was just wondering if I could be thrown a couple hints or two. Here is my source code. The problem is for inputting data from a file and outputting it to another.
Also just in case you need the information, this is the data located inside inData.txt
outFile << "Balance at the end of the month = $" << endBalance << endl;
outFile << endl;
inFile >> letter;
letter++;
outFile << "The character that comes after A in the ASCII set is "
<< letter;
inFile.close();
outFile.close();
return 0;
}
A through E consists of the header filers, declared variables, main structure of the code and the closing of the files. How do I "write a program that tests these statements"?
This program runs with no errors and compiles and runs as it should. I just don't know how to write a program that "tests" the statements.
Hmmm..... maybe you want to consult Java JUnit framework which is overkill my opinion. Idea is simple. You write classes to represent those Rectangle, Circle, BankBalance etc. In each class there will be constructors and methods that take in arguments. Then you have a outside main function that will instantiate those Rectangle, Circle and invoke their methods and the expected result you want to see. Once it matches, bingo you have tested their method.
class Rectangle {
private :
double recLength;
double recWidth;
double recArea;
double recParameter;
public:
Rectangle(double length, length width) {
recLength = length; recWidth = width;
}
publicdouble getArea();
publicdouble getPerimeter();
}
int main() {
Rectangle rect = Rectangle(5f, 5f);
if (rect.getArea() == 25f) { //please note most likely will fail as comparing double to exact
// precision will not work. You may want to overload operator == to define the exact match say
// to 2 decimal places perhaps!
cout << "rectangle area verified!\n";
}
if(rect.getPerimeter() == 20f) { //please note most likely will fail as comparing double to exact
// precision will not work. You may want to overload operator == to define the exact match say
// to 2 decimal places perhaps!
cout << "rectangle perimeter verified!\n";
}
...
}
Concept is you pass in some input parameters and then you expect some output parameters. If the implementation output something that deviate from your expected output parameters then flag error for further investigation.
If Rectangle implementation changes, your above main testing code should still give expected result unless you change your expected result logic. So those main function testing code is write once and re-run every-time the Rectangle implementation changes.