Help, this is in C++ language. We have not used void, array, stdlib.h, or sstrea
ID: 3605507 • Letter: H
Question
Help, this is in 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
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
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.