-
Notifications
You must be signed in to change notification settings - Fork 0
/
sayings1.cpp
57 lines (52 loc) · 1.2 KB
/
sayings1.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
// sayings1.cpp -- using expanded String class
// compile with string1.cpp
#include <iostream>
#include "string1.h"
const int ArSize = 10;
const int MaxLen = 81;
int main()
{
using std::cout;
using std::cin;
using std::endl;
String name;
cout << "Hi, what's your name?\n>> ";
cin >> name;
cout << name << ", please enter up to " << ArSize
<< " short sayings <empty line to quit>:\n";
String sayings[ArSize];
char temp[MaxLen];
int i ;
for (i = 0; i < ArSize; i++) {
cout << i + 1 << ": ";
cin.get(temp, MaxLen);
while (cin && cin.get() != '\n') {
continue;
}
if (!cin || temp[0] == '\0') {
break;
} else {
sayings[i] = temp;
}
}
int total = i;
cout << "Here are your sayings:\n";
for (i = 0; i < total; i++) {
cout << sayings[i][0] << ": " << sayings[i] << endl;
}
int shortest = 0;
int first = 0;
for (i = 1; i < total; i++) {
if (sayings[i].length() < sayings[shortest].length()) {
shortest = i;
}
if (sayings[i] < sayings[first]) {
first = i;
}
}
cout << "short saying:\n" << sayings[shortest] << endl;
cout << "First alphabetically:\n" << sayings[first] << endl;
cout << "This program used " << String::HowMany()
<< "String objects. Bye.\n";
return 0;
}