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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
//excercise 9-1
#include <iostream>
#include <cstring>
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
// non-interactive version:
// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
void setgolf(golf & g, const char * name, int hc);
// interactive version:
// solicits name and handicap from user
// and sets the member of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);
// function resets handicap to new value
void handicap(golf & g, int hc);
// function displays contents of the golf structure
void showgolf(const golf & g);
// a loop should solicit input from an array of golf structures and terminate when the array is full or the user enters an empty string
// for the golfer's name. you should only use these prototyped functions to access the golf structures.
int main()
{
std::cout << "enter up to 5 golfers to initialize: ";
int num_of_golfers;
int choice;
int result = 1;
int counter = 0;
const char name[] = "default name!";
int hc = 2;
while (!(std::cin >> num_of_golfers))
{
std::cin.clear();
while (std::cin.get() != '\n') // get rid of the garbage.
continue;
std::cout << "please enter a number!\n";
}
while (num_of_golfers <= 0 || num_of_golfers > 5)
{
if (!std::cin)
{
std::cin.clear();
std::cout << "please enter a number!\n";
}
else if (num_of_golfers <= 0)
{
std::cout << "\nneed to enter one or more golfers. enter a proper amoung: ";
}
else if (num_of_golfers > 5)
{
std::cout << "\ncannot have more than five golfers. enter a proper amount: ";
}
while (std::cin.get() != '\n') // get rid of the garbage.
continue;
std::cin >> num_of_golfers;
}
golf golfers[num_of_golfers];
for (int i = 0; i < num_of_golfers && result == 1; i++)
{
std::cout << "\nmake selection for golfer #" << i + 1 << std::endl;
std::cout << "1: manual entry, 2: use defaults for golfer: ";
while (std::cin.get() != '\n') // get rid of the garbage.
continue;
std::cin >> choice;
while (!std::cin) // user entered letter, cin fails.
{
std::cout << "please enter a valid number!\n";
std::cin.clear(); // must clear the instream first.
std::cout << "make selection for golfer #" << i + 1 << std::endl;
std::cout << "1: manual entry, 2: use defaults for golfer: ";
while (std::cin.get() != '\n') // get rid of the garbage.
continue;
std::cin >> choice; // try again.
}
while (choice < 1 || choice > 2)
{
if (!std::cin)
{
std::cin.clear();
std::cout << "please enter a number!\n";
}
else if (choice < 1)
{
std::cout << "number cannot be lower than 1! please select a valid choice\n";
}
else if (choice > 2)
{
std::cout << "number cannot be greater than 2! please select a valid choice\n";
}
while (std::cin.get() != '\n') // get rid of the garbage.
continue;
std::cin >> choice;
}
if (choice == 1)
{
result = setgolf(golfers[i]);
counter++;
}
else
{
setgolf(golfers[i], name, hc);
counter++;
}
if (result == 0)
break;
}
for (int i = 0; i < counter; i++)
{
showgolf(golfers[i]);
}
std::cout << "press r to reset all the golfers to default handicap: ";
char reset_golfers;
std::cin >> reset_golfers;
if (reset_golfers == 'r' || reset_golfers == 'R')
{
std::cout << "\ngolfers reset!\n";
for (int i = 0; i < counter; i++)
{
if (strcmp(golfers[i].fullname, name))
handicap(golfers[i], 2);
}
}
std::cout << "\n\nprogram exiting!\n";
return 0;
}
void setgolf(golf & g, const char * name, int hc)
{
std::cout << "\nnon-interactive version function called! using defaults!.\n";
strcpy(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf & g)
{
std::cout << "\ninteractive function called! manual entry mechanism.\n";
std::cout << "\nenter a name (empty line to quit)\n";
std::cout << ": ";
while (std::cin.get() != '\n') // get rid of the garbage.
continue;
std::cin.get(g.fullname, 40);
if (g.fullname[0] == '\0')
return 0;
else
{
std::cout << "\nenter the handicap: ";
std::cin >> g.handicap;
while (!std::cin)
{
std::cin.clear();
while (std::cin.get() != '\n')
continue;
std::cout << "\nplease enter a number!\n";
std::cout << ": ";
std::cin >> g.handicap;
}
return 1;
}
}
void handicap(golf & g, int hc)
{
char name3[50] = "defalut name";
std::cout << "\nreseting handicap of " << g.fullname << " to " << hc << "!\n";
g.handicap = hc;
}
void showgolf(const golf & g)
{
std::cout << "\nshowing the contents of golfer " << g.fullname << "\n";
std::cout << g.handicap << " handicap value." << std::endl;
}
|