Question on how to write a program to test statements

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

10.20 5.35
15.6
Randy Gill 31
18500 3.5
A


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
ifstream inFile;
ofstream outFile;

inFile.open("C:\\inData.txt");
outFile.open("C:\\outData.txt");

// Rectangle variables
double recLength;
double recWidth;
double recArea;
double recParameter;

// Circle variables
double circRadius;
double circArea;
double circCircum;

// Identification variables
int age;
string firstName;
string lastName;

// Bank Variables
double beginBalance;
double endBalance;
double interestRate;

// Char Variables
char letter;

inFile >> recLength; inFile >> recWidth;

recArea = recLength * recWidth;
recParameter = (recLength * 2) + (recWidth * 2);

outFile << "Rectangle:" << endl;
outFile << "Length = " << setprecision(2) << fixed << recLength << ", "
<< "width = " << recWidth << ", " << " area = " << recArea
<< ", " << "parameter = " << recParameter << endl;

outFile << endl;

inFile >> circRadius;

circArea = 3.1416 * (circRadius * circRadius);
circCircum = 3.1416 * (circRadius * 2);

outFile << "Circle:" << endl;
outFile << "Radius = " << circRadius << ", " << "area = "
<< circArea << ", " << "circumference = "
<< circCircum << endl;
outFile << endl;

inFile >> firstName;
inFile >> lastName;
inFile >> age;


outFile << "Name: " << firstName << " " << lastName
<< ", " << "age: " << age << endl;

inFile >> beginBalance; inFile >> interestRate;

outFile << "Beginning balance = $" << beginBalance
<< ", " << "interest rate = " << interestRate
<< " " << endl;

interestRate = interestRate / 100;
endBalance = ( (beginBalance * interestRate) / 12 ) + beginBalance;

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.

Help is much appreciated.

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

E.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Rectangle {
private :
double recLength;
double recWidth;
double recArea;
double recParameter;

public:
Rectangle(double length, length width) {
  recLength = length; recWidth = width;
}
public double getArea();
public double 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.
Last edited on
Topic archived. No new replies allowed.