Saving Numbers to a File
This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (3) was titled “Expressions and Interactivity”.
I post these old academic challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it.
3.21: Write two programs:
Program 1: Write a program that asks user to enter five numbers. Use a floating-point data type to hold those numbers. The program should create a file and save all five numbers to the file.
Program 2: Write a program that opens the file created by Program 1, reads the five numbers, and displays them. The program should also calculate and display the sum of the five numbers.
My solution for Program 1:
/* Chapter 3 Extra Credit 1
Written by Kirk Hingsberger
September 26, 2007
Saving Numbers to a File, Program 1
*/
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
float num1, num2, num3, num4, num5;
cout << "Enter five numbers, each separated by a space (decimals allowed)" << endl;
cin >> num1 >> num2 >> num3 >> num4 >> num5;
ofstream outputFile;
outputFile.open("demofile.txt");
cout << "Now writing those five numbers to demofile.txt file." << endl;
outputFile << num1 << endl;
outputFile << num2 << endl;
outputFile << num3 << endl;
outputFile << num4 << endl;
outputFile << num5 << endl;
outputFile.close();
cout << "Finished writing those five numbers to demofile.txt file." << endl;
return 0;
}
My solution for Program 2:
/* Chapter 3 Extra Credit 2
Written by Kirk Hingsberger
September 26, 2007
Saving Numbers to a File, Program 2
*/
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
float num1, num2, num3, num4, num5, sum;
ifstream inFile;
inFile.open("demofile.txt");
cout << "Now reading information from demofile.txt..." << endl;
inFile >> num1;
cout << "num1 is " << num1 << endl;
inFile >> num2;
cout << "num2 is " << num2 << endl;
inFile >> num3;
cout << "num3 is " << num3 << endl;
inFile >> num4;
cout << "num4 is " << num4 << endl;
inFile >> num5;
cout << "num5 is " << num5 << endl;
inFile.close();
cout << "Closed demofile.txt file." << endl;
sum = num1 + num2 + num3 + num4 + num5;
cout << "The sum of these five numbers are " << sum << endl;
return 0;
}
Here is the capture script I turned in for grading (Program 1):
Script started on Thu 27 Sep 2007 01:32:24 PM MDT
$ cat chap3Extra1.cc
/* Chapter 3 Extra Credit 1
Written by Kirk Hingsberger
September 26, 2007
Saving Numbers to a File, Program 1
*/
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
float num1, num2, num3, num4, num5;
cout << "Enter five numbers, each separated by a space (decimals allowed)" << endl;
cin >> num1 >> num2 >> num3 >> num4 >> num5;
ofstream outputFile;
outputFile.open("demofile.txt");
cout << "Now writing those five numbers to demofile.txt file." << endl;
outputFile << num1 << endl;
outputFile << num2 << endl;
outputFile << num3 << endl;
outputFile << num4 << endl;
outputFile << num5 << endl;
outputFile.close();
cout << "Finished writing those five numbers to demofile.txt file." << endl;
return 0;
}
$ g++ -o chap3Extra1 chap3Extra1.cc -s
$ chap3Extra1
Enter five numbers, each separated by a space (decimals allowed)
5 55 555 55.55 5.5555
Now writing those five numbers to demofile.txt file.
Finished writing those five numbers to demofile.txt file.
$ exit
exit
Script done on Thu 27 Sep 2007 01:33:09 PM MDT
Here is the capture script I turned in for grading (Program 2):
Script started on Thu 27 Sep 2007 01:33:18 PM MDT
$ cat chap3Extra2.cc
/* Chapter 3 Extra Credit 2
Written by Kirk Hingsberger
September 26, 2007
Saving Numbers to a File, Program 2
*/
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
float num1, num2, num3, num4, num5, sum;
ifstream inFile;
inFile.open("demofile.txt");
cout << "Now reading information from demofile.txt..." << endl;
inFile >> num1;
cout << "num1 is " << num1 << endl;
inFile >> num2;
cout << "num2 is " << num2 << endl;
inFile >> num3;
cout << "num3 is " << num3 << endl;
inFile >> num4;
cout << "num4 is " << num4 << endl;
inFile >> num5;
cout << "num5 is " << num5 << endl;
inFile.close();
cout << "Closed demofile.txt file." << endl;
sum = num1 + num2 + num3 + num4 + num5;
cout << "The sum of these five numbers are " << sum << endl;
return 0;
}
$ g++ -o chap3Extra2 chap3Extra2.cc -s
$ chap3Extra2
Now reading information from demofile.txt...
num1 is 5
num2 is 55
num3 is 555
num4 is 55.55
num5 is 5.5555
Closed demofile.txt file.
The sum of these five numbers are 676.105
$ exit
exit
Script done on Thu 27 Sep 2007 01:33:46 PM MDT










