String Dates

Hi, I need help with 2 (mostly the 2nd question) Questions: 1) I have to program a code where you enter n number of sales dates on the form dd-mm-yyyy. These dates must be saved in a .txt 2) Here the dates must be downloaded from the file .txt where "Δ𝑛" (= the number of days between a sales date and the previous one) and dn [NMAX] (integer array whose contents are the N calculated values of Δ𝑛.)

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
34
35
36
37
38
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string.h>

void TastOgGemDatoer(int &N);
const int NMAX=40;
using namespace std;

int main() {
	int N
	
TastOgGemDatoer(N);


	return 0;
}


void TastOgGemDatoer(int &N){

	char strengA[11]="31.02.2021";
	ofstream fil;

	cout<<"\n Enter total amount of sales N=";
	cin>>N;

	fil.open("SalgsDatoer.txt");
	fil<<strengA<<endl;

	for(int k=0; k<=N; k++){
		cout <<"\nEnter dates of sales"<<k<<"=";
		cin.getline(strengA,11);
		fil<<strengA<<endl;
	}
	fil.close();
}


This is what i have for question one
For 1), perhaps consider:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
#include <cstring>

using namespace std;

const int NMAX {40};

int TastOgGemDatoer();

int getInt(const std::string& prm)
{
	const auto notsp {[&]() {while (std::isspace(std::cin.peek()) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }};
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || notsp())) {
		std::cout << "Not an integer\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

int main() {
	const auto N {TastOgGemDatoer()};
}

bool checkDate(const char* date)
{
	// NOT A COMPLETE CHECK FOR VALID DATE
	char* endd {}, * endm {}, * endy {};

	if (strlen(date) != 10)
		return false;

	if (const auto dy {strtol(date, &endd, 10)}; dy < 1 || dy > 31 || *endd != '-')
		return false;

	if (const auto mn {strtol(endd + 1, &endm, 10)}; mn < 1 || mn > 12 || *endm != '-')
		return false;

	const auto yr {strtol(endm + 1, &endy, 10)};

	return !(yr < 1900 || yr > 2030 || *endy != 0);
}

int TastOgGemDatoer()
{
	char strengA[11] {};
	ofstream fil("SalgsDatoer.txt");
	int N {};

	if (fil) {
		do {
			N = getInt("Enter total amount of sales N= ");
		} while ((N < 1 || N > NMAX) && (cout << "Invalid N\n"));

		for (int k = 0; k < N; ++k) {
			while ((cout << "\nEnter date of sale (dd-mm-yyyy)" << k + 1 << "= ") && cin.getline(strengA, 11) && !checkDate(strengA))
				cout << "Invalid date\n";

			fil << strengA << '\n';
		}
	} else
		cout << "Cannot open file\n";

	return N;
}


For 2), what is the actual format of the file. Please provide a sample.

Once the dates have been downloaded from the file, what is then required?
Topic archived. No new replies allowed.