-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
78 lines (68 loc) · 2.59 KB
/
main.cpp
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
73
74
75
76
77
78
/*
Developer's Name: Joshua Perry
Date: 10/14/2023
Purpose: Systematically track and analyze the purchase frequency of grocery items, providing insights to optimze store layout and enhance customer shopping experience
*/
#include "GroceryTracker.h"
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
int main() {
GroceryTracker tracker;
std::ifstream infile("C:\\Users\\jperr\\source\\repos\\Project3\\x64\\Debug\\CS210_Project_Three_Input_File.txt"); // Update to local location of input file
std::string item;
// Check if the file can be opened
if (!infile.is_open()) {
std::cerr << "Error: Could not open input file\n";
return 1; // non-zero return indicates an error
}
// Reading items from file and adding them to tracker
while (infile >> item) {
tracker.addItem(item);
}
infile.close();
// Main loop for user interaction
int option = 0;
while (option != 4) {
std::cout << "1. Get item frequency\n2. Print all frequencies\n3. Print histogram\n4. Exit\n";
// Ensure input is an integer
while (!(std::cin >> option)) {
std::cin.clear(); // clear error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // discard invalid input
std::cout << "Invalid input. Please enter a numeric value between 1 and 4: ";
}
// Ensure input is within the acceptable range
while (option < 1 || option > 4) {
std::cout << "Invalid choice. Please enter a number between 1 and 4: ";
// Ensure input is an integer }
while (!(std::cin >> option)) {
std::cin.clear(); // clear error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // discard invalid input
std::cout << "Invalid input. Please enter an integer: ";
}
}
switch (option) {
case 1:
std::cout << "Enter item: ";
std::cin >> item;
std::cout << item << ": " << tracker.getItemFrequency(item) << "\n";
break;
case 2:
tracker.printAllFrequencies();
break;
case 3:
tracker.printHistogram();
break;
case 4:
std::cout << "Exiting...\n";
break;
default:
std::cout << "Invalid option\n";
break;
}
}
// Save to file upon exit
tracker.saveToFile("frequency.dat");
return 0;
}