Description
- Assume that each of the following statement applies to the same program.
- Write a statement that opens file dat for input; use an ifstream object called inTransaction.
- Write a statement that opens file dat for input; use an ifstream object called inBalance.
- Write a statement that reads integer accountNumber, floating-point amount from the file dat; use ifstream object inTransaction.
- Write a statement that reads integer accountNumber, string name, floating-point currentBalance from the file dat; use ifstream object inBalance.
- Write a statement that writes integer accountNumber, string name, updated balance which is a floating-point currentBalance + amount to the file dat; use ifstream object inBalance.
- Suppose that numStudents is an int variable and classCode is a string What are the values of numStudents and classCode after the following input statements execute:
cin >> numStudents;
getline(cin, classCode);
if the input is:
- 80 ENGG1111
- 80
ENGG1111
- The following program is supposed to read two numbers from a file named dat and write the product of the numbers to a file named product.dat. However, it fails to accomplish the task. Fix by rewriting the program so that it performs what it is supposed to do.
#include <iostream> #include <fstream> using namespace std;
int main()
{
int num1, num2; ifstream infile;
outfile.open(“product.dat”); infile >> num1 >> num2;
outfile << “Product = ” << num1 * num2 << endl; return 0;
}
Self-Review Exercise Module 7 p. 1/2
- Consider the following statements:
struct movieType
{
string title; string genre; int year; double rating;
};
movieType movies[100];
movieType oldMovie;
State if each of the following statements is valid or invalid. If a statement is invalid, explain why.
- cout << oldMovie.name;
- year = 2015; (c) movies[11] = oldMovie;
(d) oldMovie.title = “Titanic”; (e) if (movies[99].genre == “drama”)
movies[99].rating = 3.5;
- The following program calculates the summation of first n natural numbers. E.g., 𝑖𝑓 𝑛 = 6, 𝑠𝑢𝑚 =
1 + 2 + 3 + 4 + 5 + 6 = 21. Rewrite the sum() that uses recursion to calculate and return the sum of first n.
#include <iostream> using namespace std;
// iterative version int sum(int n)
{
int sum = 0;
for (int i = 1; i <= n; ++i)
sum += i;
return sum;
}
int main()
{
int n;
cout << “Enter a positive integer: “; cin >> n;
cout << “Sum of first ” << n << ” natural numbers = ” << sum(n) << endl;
return 0;
}
Self-Review Exercise Module 7 p. 2/2




