Skip to content

Commit c427d34

Browse files
committed
update 6.11 all assignments
1 parent ee028c6 commit c427d34

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <string>
4+
5+
int main()
6+
{
7+
int counter = 0;
8+
9+
const std::string filename = "words.txt";
10+
11+
std::ifstream inFile;
12+
char ch;
13+
inFile.open(filename);
14+
inFile >> ch;
15+
while(!inFile.eof())
16+
{
17+
++counter;
18+
inFile >> ch;
19+
std::cout << ch << std::endl;
20+
}
21+
if (inFile.eof())
22+
{
23+
std::cout << "End of file reached." << std::endl;
24+
}
25+
26+
std::cout << counter << std::endl;
27+
inFile.close();
28+
29+
return 0;
30+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <string>
4+
#include <cstdlib>
5+
6+
struct patron
7+
{
8+
std::string name;
9+
double donation;
10+
};
11+
12+
13+
const int Maxnum = 5;
14+
15+
16+
int main()
17+
{
18+
const std::string filename = "patrons.txt";
19+
std::fstream inFile;
20+
21+
inFile.open(filename);
22+
if(!inFile.is_open())
23+
{
24+
std::cout << "Could not open the file" << std::endl;
25+
std::cout << "Program terminating" << std::endl;
26+
27+
std::exit(EXIT_FAILURE);
28+
}
29+
30+
char line[100];
31+
inFile.getline(line, 100);
32+
const int num = atoi(line);
33+
patron** parPtr = new patron*[num];
34+
std::string lines[num * 2];
35+
36+
int index = 0;
37+
38+
while(inFile.good())
39+
{
40+
inFile.getline(line, 100);
41+
lines[index] = line;
42+
index++;
43+
// std::cout << line << std::endl;
44+
}
45+
46+
if (inFile.eof())
47+
{
48+
std::cout << "End of file reached. " << std::endl;
49+
50+
}
51+
inFile.close();
52+
53+
for(int i = 0; i < index - 1; i += 2)
54+
{
55+
patron* paPtr = new patron();
56+
paPtr->name = lines[i];
57+
paPtr->donation = atoi(lines[i + 1].c_str());
58+
parPtr[i / 2] = paPtr;
59+
}
60+
61+
std::cout << "Grand Patrons: " << std::endl;
62+
int check = 0;
63+
for(int i =0; i < num; i++)
64+
{
65+
if (parPtr[i]->donation > 10000){
66+
std::cout << parPtr[i]->name << ": " << parPtr[i]->donation << std::endl;
67+
check += 1;
68+
}
69+
}
70+
71+
if (check == 0)
72+
{
73+
std::cout << "None" << std::endl;
74+
}
75+
76+
check = 0;
77+
std::cout << "Patrons: " << std::endl;
78+
for(int i=0; i < num; i++)
79+
{
80+
if(parPtr[i]->donation < 10000)
81+
{
82+
std::cout << parPtr[i]->name << ": " << parPtr[i]->donation << std::endl;
83+
check += 1;
84+
}
85+
}
86+
if (check == 0)
87+
std::cout << "None" << std::endl;
88+
89+
for(int i = 0; i < num; i++)
90+
{
91+
delete parPtr[i];
92+
}
93+
94+
delete[] parPtr;
95+
96+
return 0;
97+
}

chapter06/assignment/patrons.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
4
2+
Sam Stone
3+
2000
4+
Freida Flass
5+
10050
6+
Tammy Tubbs
7+
5000
8+
Rich Raptor
9+
55000

chapter06/assignment/words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world, this is a test file.

0 commit comments

Comments
 (0)