-
Notifications
You must be signed in to change notification settings - Fork 0
/
sayings2.cpp
65 lines (60 loc) · 1.46 KB
/
sayings2.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
// sayings2.cpp -- uses pointers to objects
// compile with string1.cpp
#include <iostream>
#include <cstdlib> // (or stdlib.h) for rand(), srand()
#include <ctime>
#include "string1.h"
const int ArSize = 10;
const int MaxLen = 81;
int main()
{
using namespace std;
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]; // temporary string storage
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;
if (total > 0) {
cout << "Here are your sayings: \n";
for (i = 0; i < total; i++) {
cout << sayings[i] << "\n";
}
String * shortest = &sayings[0];
String * first = &sayings[0];
for (i = 1; i < total; i++) {
if (sayings[i].length() < shortest->length()) {
shortest = &sayings[i];
}
if (sayings[i] < *first) {
first = &sayings[i];
}
}
cout << "Shortest saying: \n" << * shortest << endl;
cout << "First alphabetically: \n" << * first << endl;
srand(time(0));
int choice = rand() % ArSize;
String * favorite = new String(sayings[choice]);
cout << "My favorite saying: \n" << *favorite << endl;
delete favorite;
} else {
cout << "No much to say, eh?\n";
cout << "Bye.\n";
}
return 0;
}