studying c++

Sticky

# include <iostream>
# include <cstdlib>
# include <ctime>
# include “string1.h”
using namespace std;

const int arSize = 10;
const int maxLen = 81;

int main(){
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])
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”;
//use pointers to keep track of shortest ,first strings
String* shortest = &sayings[0]; //initialize to first object
String* first = &sayings[0];
for (i = 1; i < total; i++){
if (sayings[i].length() < shortest->length())
shortest = &sayings[0];
if (sayings[i] < *first)
first = &sayings[i];
}
cout << “Shortest sayin:\n” << *shortest << endl;
cout << “First alphabetically:\n” << *first << endl;
srand(time(0));
int choice = rand() % total; //pick index at random

String* favorite = new String(sayings[choice]);
cout << “My favorite saying:\n” << *favorite << endl;
delete favorite;
}
else
cout << “Not much to say,eh?\n”;
cout << “Bye.\n”;
return 0;
}

badger.cpp

Standard

/class Badger{
// char* _strogage;
//public:
// Badger();
// Badger(const char*);
// Badger(const Badger&);
// ~Badger();
//
// void set(const char*);
// void print() const;
// int length() const;
// Badger& operator=(const Badger&);
//};
//
//# include <iostream>
//# include <cstring>
//using namespace std;
//
//Badger::Badger(){
// _strogage = 0;
//}
//Badger::Badger(const char* str){
// set(str);
//}
//Badger::Badger(const Badger& rg){
// if (rg._strogage){
// _strogage = new char[strlen(rg._strogage) + 1];
// strcpy(_strogage,rg._strogage);
// }
// else{
// _strogage = 0;
// }
//}
//void Badger::set(const char* s){
// if (_strogage){
// delete[] _strogage;
// _strogage = NULL;
// }
// if (s){
// _strogage = new char[strlen(s) + 1];
// strcpy(_strogage, s);
// }
//}
//int Badger::length()const{
// if (_strogage){
// return strlen(_strogage);
// }
// else
// return 0;
//}
//void Badger::print()const{
// if (_strogage)
// cout << _strogage;
//}
//Badger& Badger::operator=(const Badger& rg){
// if (this==&rg)
// return *this;
// delete[] this;
// _strogage = new char[strlen(rg._strogage) + 1];
// strcpy(_strogage, rg._strogage);
// return *this;
//}