-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hw5 - Enumerated Type || Intermediate C and C++
91 lines (77 loc) · 2.17 KB
/
Hw5 - Enumerated Type || Intermediate C and C++
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
//main.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#define INF "input.txt"
using namespace std;
int main(){
cout << "Welcome to Enumerated Types by Eric Garcia\n" << endl;
//Opening input file
ifstream dataFile(INF);
if(!dataFile){
cerr << "Can't open " << INF << " for input" << endl;
exit(1);
}
enum check {ISNUMBER, ISLETTER, ISSYMBOL, ISWHITESPACE};
vector<int> numbers;
vector<char> letters;
vector<char> symbols;
//Loop to read data and put it into vectors
for(size_t i = 0; !dataFile.eof(); ++i){
//Reads input.txt and assigns it to input
char input;
check data;
input = dataFile.get();
//Assigns the enum check a value based on what the input is and assigns it to the right vector
if(isdigit(input)){
data = ISNUMBER;
}
else if(isalpha(input)){
data = ISLETTER;
}
else if(isspace(input)){
data = ISWHITESPACE;
}
else {
data = ISSYMBOL;
}
//Prints out a statement dependent on what the enum data is
if(data == ISNUMBER){
cout << "A number was read: " << input << endl;
input = (int)input; //makes input an integer
input -= 48; //gets the correct value of the integer
numbers.push_back(input);
}
else if(data == ISLETTER){
cout << "A letter was read: " << input << endl;
letters.push_back(input);
}
else if(data == ISSYMBOL){
cout << "A symbol was read: " << input << endl;
symbols.push_back(input);
}
}
//Loops to print table
cout << "\n\nThe Numbers are: ";
for(size_t i = 0; i < numbers.size(); ++i){
cout << numbers[i] << ", ";
}
cout << "\n\nThe Letters are: ";
for(size_t i = 0; i < letters.size(); ++i){
cout << letters[i] << ", ";
}
cout << "\n\nThe Symbols are: ";
for(size_t i = 0; i < symbols.size(); ++i){
cout << symbols[i] << ", ";
}
cout << endl;
dataFile.close();
}
//----------------------------------------------------------------------------------------------------------------------------
//input.txt
input.txt
Created on: May 15, 2020
Author: LPage
dnsjlgfhdjl894569486^#@%@%&^