C++ language. We have not used void, array, stdlib.h, or sstream so please do no
ID: 3605595 • Letter: C
Question
C++ language. We have not used void, array, stdlib.h, or sstream so please do not use that. Also, we have only learned basics so the program is suppose to only use loops to open and output the data. Our professor hinted that it would require many variables.
Write a program that reads a person’s physical activity from a file, collects statistics by month, and prints a table showing those statistics.
The program should prompt the user for the name of the file. This file contains information on the number of steps a person has taken on a given day. Each line of the file is of the form #1/#2 #3 where:
#1 is the one or two-digit month, ranging from 1 to 12
#2 is the one or two-digit day of the month, ranging from 1 to 31
#3 is the number of steps the person walked that day
If a line in the file contains something other than data of this format, that line should be ignored and a warning printed to the user about the ignored line. White space between values are not considered relevant and should not impact the validity of a line. Don’t worry about the same day appearing multiple times in the file... that is guaranteed not to happen. Lines consisting of only whitespace should be ignored.
While reading the file, the program should capture and calculate the minimum number of steps taken in each month and the day those steps were taken, the maximum number of steps taken in each month and the day those steps were taken, the total number of steps taken in each month, and the average number of steps taken each day in a month (only for the days that have steps provided... don’t include days without steps in the average).
After reading all the data in the file, the program should print a nicely formatted table with all the statistics gathered and calculated.
Your program should validate the data as much as possible. In addition, the program should recover from unexpected input as much as possible.
Do NOT use arrays in this program.
HINT: Start with input files that contain only well-formed, valid input. Then gradually introduce invalid data into the input file and ensure your program handles it as well as possible.
Here is a sample input file for the program. This sample input file has some issues!
a
2a
3/4a
1/1 10001
1/2 9002
1/4 4300
3/1 4500
3/2 15412
4/1 3500
9/8 3456
12/25 1
12/26 0
3/5 5000a
2/3 3000
5 / 8 900
3/6 2123
6b 7/8 c 9/
Here is the sample output of the professor’s solution for this project.
Please enter the relative or absolute path of the input file: input1.txt
Unable to read month (or reached end-of-file) from line 1 of the input file Did not find expected slash from line 2 of the input file
Unable to read activity from line 3 of the input file
Invalid step count (or end-of-file) from line 12 of the input file Unexpected characters found on line 13 of the input file
Did not find expected slash from line 17 of the input file
Unable to read activity from line 18 of the input file
Unable to read day (or reached end-of-file) from line 19 of the input file
|---Maximum---|---Minimum---|---Total---|---Average---|
|-Day---Count-|-Day---Count-|---Steps---|----Steps----|
================================================================
| January | 1| 10001| 4| 4300| 23303| 7767.67|
================================================================
| February | 3| 3000| 3| 3000| 3000| 3000|
================================================================
| March | 2| 15412| 6| 2123| 22035| 7345|
================================================================
| April | 1| 3500| 1| 3500| 3500| 3500|
================================================================
| May | 8| 900| 8| 900| 900| 900|
================================================================
| June | | | | | | |
================================================================
| July | | | | | | |
================================================================
| August | | | | | | |
================================================================
| September | 8| 3456| 8| 3456| 3456| 3456|
================================================================
| October||||| | |
================================================================
| November||||| | |
================================================================
| December | 25| 1| 25| 1| 1| 1|
================================================================|
Explanation / Answer
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
//inputting the file name
string filename;
cout << "Enter a file name: ";
cin >> filename;
//opening the file for reading
ifstream in(filename);
if(!in) {
cout << "Cannot open input file. ";
return 1;
}
//reading the file
char str[255];
cout << "Date # of Steps" << endl;
while(in) {
in.getline(str, 255);
//splitting the line contents
string s = str;
string delim = " ";
auto start = 0U;
auto end = s.find(delim);
while (end != string::npos){
cout << s.substr(start, end - start);
start = end + delim.length();
end = s.find(delim, start);
}
//printing the result
cout << " " << s.substr(start, end) << endl;
}
in.close();
return 0;
}
Sample
Enter a file name: input.txt
Date # of Steps
12/2 123
12/3 456
12/4 345
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.