-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
183 lines (169 loc) · 6.52 KB
/
main.cpp
File metadata and controls
183 lines (169 loc) · 6.52 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/******************************************************************************
# Author: Kate Stingle
# Assignment: Assignment 2 (CS 161B)
# Date: January 18th, 2025
# Description: - The goal of this program is to take some user input about a student’s assignment submission
and generate a file name for their submission. There will always be a welcome and exit
message, and the program uses input validation.
# Input: In ReadOption(char &option):
- option as char& reference.
In ReadInput(char fName[], char lName[], bool &lateFlag):
- fName and lName as char[] arrays (to be read to)
- yesNo as char (to be read to, for lateFlag t/f)
In ReadInput(char parsedID[], char fileName[]):
- stdID (student ID, last 4 chars copied to parsedID) and fileName as char[]
arrays (to be read to)
In ReadTime(char strTime[]):
- hr (0-24) and min (0-60) as ints (to be read to)
- discard as char (for “:”)
- strTime as char[] array (not to be read to, but edited)
# Output: - ERR_INPUT_MSG as const string = “Invalid option! Please try again!!”
- outputFileName[] as Char array (the produced file name based on user input, passed
in and generated by the encode(char encodeFileName[]) function
# Sources: Assignment 2 Specifications
#******************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
using namespace std;
// CONSTANTS
const string ERR_INPUT_MSG = "Invalid option! Please try again!!";
const string WELCOME_MSG = "Welcome to my fileName encoding program!!";
const string EXIT_MSG = "Thank you for using my fileName generator!";
const string PRGRM_DESC_MSG = "This program will ask you a few questions and generate an encoded fileName based on your answers.";
const string LNAME_PROMPT_MSG = "Enter your last name: ";
const string FNAME_PROMPT_MSG = "Enter your first name: ";
const string LATE_PROMPT_MSG = "Was your assignment Late (y/n)? ";
const string STUDENTID_PROMPT_MSG = "Enter your Student-ID (format: 222-22-2222): ";
const string FILENAME_PROMPT_MSG = "Enter the file name: ";
const string TIME_PROMPT_MSG = "Enter the time submitted (military time - ex: 18:24 for 6:24pm): ";
const int MIN_IN_HR = 60;
const int HR_IN_DAY = 24;
const int MAXCHAR = 50;
// FUNCTION HEADERS
void welcome();
void displayMenu();
void readOption(char& option);
void readInput(char fName[], char lName[], bool& lateFlag);
void readInput(char parsedID[], char fileName[]);
void readTime(char strTime[]);
void encode(char outputFileName[]);
void cStrToLower(char cStr[]);
// MAIN
int main() {
char option;
char outputFileName[MAXCHAR];
welcome();
displayMenu();
readOption(option); // encode a file name or quit
while (option == 'e') {
encode(outputFileName); //prompts user for info and constructs name
cout << "Your encoded file name is: " << outputFileName << endl;
displayMenu(); //reprompts to continue making file names or quit
readOption(option);
}
cout << EXIT_MSG << endl;
return 0;
}
// DEFINE FUNCTIONS
void welcome() {
cout << WELCOME_MSG << endl;
}
void displayMenu(){
cout << "Please pick an option below : " << endl;
cout << "(e)Encode a file name" << endl;
cout << "(q)quit" << endl;
}
void readOption(char& option) {
cin >> option;
//input validation:
while (cin.fail() || !isalpha(option) || (tolower(option) != 'e' && tolower(option) != 'q')) {
cout << ERR_INPUT_MSG << endl; // error message
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //clear buffer and lower fail flag
cin >> option; // reinput
}
}
// prompts user info & generates password to outputFileName[]
void encode(char outputFileName[]){
char fName[MAXCHAR], lName[MAXCHAR], parsedID[MAXCHAR];
char inputFileName[MAXCHAR], strTime[MAXCHAR];
bool lateFlag;
cout << PRGRM_DESC_MSG << endl; // output program function description
readInput(fName, lName, lateFlag); // prompt and read for first & last name & ask if late
readInput(parsedID, inputFileName); // prompt for student id and file name
readTime(strTime); // prompt for time submitted in military time
// construct the file name in outputFileName
strcpy(outputFileName, lName);
strcat(outputFileName, "_");
strcat(outputFileName, fName); // FIX THIS more optimal????????????????
strcat(outputFileName, "_");
if (lateFlag) { // add LATE if late
strcat(outputFileName, "LATE_");
}
strcat(outputFileName, parsedID);
strcat(outputFileName, "_");
strcat(outputFileName, strTime);
strcat(outputFileName, "_");
strcat(outputFileName, inputFileName);
}
//collects first, last name and asks if late
void readInput(char fName[], char lName[], bool& lateFlag) {
char yesNo;
cout << FNAME_PROMPT_MSG;
cin >> fName;
cStrToLower(fName);
cout << LNAME_PROMPT_MSG;
cin >> lName;
cStrToLower(lName);
cout << LATE_PROMPT_MSG;
cin >> yesNo;
yesNo = tolower(yesNo);
while(yesNo != 'y' && yesNo != 'n'){
cout << ERR_INPUT_MSG << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //clear buffer and lower fail flag
cin >> yesNo;
}
if (yesNo == 'y') {
lateFlag = true;
} else{
lateFlag = false;
}
}
// collect student ID and file name
void readInput(char parsedID[], char fileName[]){
char stdID[MAXCHAR];
cout << STUDENTID_PROMPT_MSG;
cin >> stdID;
strncpy(parsedID, stdID + 8, 4);
parsedID[4] = '\0'; // Ensure null termination
cout << FILENAME_PROMPT_MSG;
cin >> fileName;
}
//collect submission time
void readTime(char strTime[]) {
int hr, min;
char discard;
cout << TIME_PROMPT_MSG;
cin >> hr >> discard >> min; //discard holds the ":" between the hours and the mins
while(cin.fail() || hr < 0 || hr >= 24 || min < 0 || min >= 60 || discard != ':') {
cout << ERR_INPUT_MSG << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //clear buffer and lower fail flag
cout << TIME_PROMPT_MSG;
cin >> hr >> discard >> min;
}
//convert hour from int to string then to cstring and copy to timeStr
strncpy(strTime, to_string(hr).c_str(), 10);
//convert min from int to string and then cstring and concatenate to timeStr
strcat(strTime, to_string(min).c_str());
}
//goes through each character in a char array and changes to lower
void cStrToLower(char cStr[]){
for (int i = 0; i < strlen(cStr) ; i++) {
cStr[i] = tolower(cStr[i]);
}
}