char numberToLetterGrade(int a){ if (a <=100 && a >= 90) { return 'A'; }else if (a<=89 && a>=79){ return 'B'; }else if (a<=78 && a>=70){ return 'C'; }else if (a<70){ return 'F'; }else{ return 'X'; } }
char numberToLetterGrade(int a) { if (a > 100) { return 'X'; } if (a >= 90) { return 'A'; } if (a >= 79) { return 'B'; } if (a >= 70) { return 'C'; } return 'F'; }
string restaurantList::getInput(){ //gets an input with some error checking //string value; char value[50]; string toReturn = ""; int tries = 0; bool good_input = false; do{ cin.getline(value, 50, '\n'); //cout << "\nvalue: '" << value << "'\n"; if (value[0] == '\0'){ //Someties there are newline characters from the previous input (entering a selection and pressing return). This ignores that. //cout << "\ndetected null character, retrying...\n"; cin.getline(value, 50, '\n'); } if(!cin){ cout << "Invalid input, try again!\n>> "; ++tries; } else{ good_input = true; } }while(tries < 3 && !good_input); if (!good_input){ cout << "Invalid input, returning empty string...\n"; } else{ toReturn = value; //cast the char array to a proper string and return it } return toReturn; }
string restaurantList::getInput() const { string value; int tries = 0; do { cout << ">> "; getline(cin, value); if (cin) { return value; } cerr << "Invalid input, try again!\n"; } while(++tries < 3); cerr << "Invalid input, returning empty string...\n"; return ""; }
int main() { //Construct rectangles with a name and dimensions: rectangle wall(12, 10); rectangle square(8, 8); rectangle door(7, 3); rectangle window(4, 3); rectangle table(3, 2); rectangle billboard(11, 8); cout << "--------------------------------------------\n" << "The area of the wall is " << wall.area() << " square units.\nThe perimeter of the wall is " << wall.perimiter() << " units.\nThe diagonal of the wall is " << wall.diagonal() << "units.\n" << "--------------------------------------------\n" << "The area of the square is " << square.area() << " square units.\nThe perimeter of the square is " << square.perimiter() << " units.\nThe diagonal of the square is " << square.diagonal() << "units.\n" << "--------------------------------------------\n" << "The area of the door is " << door.area() << " square units.\nThe perimeter of the door is " << door.perimiter() << " units.\nThe diagonal of the door is " << door.diagonal() << "units.\n" << "--------------------------------------------\n" << "The area of the window is " << window.area() << " square units.\nThe perimeter of the window is " << window.perimiter() << " units.\nThe diagonal of the window is " << window.diagonal() << "units.\n" << "--------------------------------------------\n" << "The area of the table is " << table.area() << " square units.\nThe perimeter of the table is " << table.perimiter() << " units.\nThe diagonal of the table is " << table.diagonal() << "units.\n" << "--------------------------------------------\n" << "The area of the billboard is " << billboard.area() << " square units.\nThe perimeter of the billboard is " << billboard.perimiter() << " units.\nThe diagonal of the billboard is " << billboard.diagonal() << "units.\n"; return EXIT_SUCCESS; }
An array of structures:
struct thing { string name; rectangle rect; }; int main() { const thing a[] = { {"wall", rectangle(12, 10)}, {"square", rectangle( 8, 8)}, {"door", rectangle( 7, 3)}, {"window", rectangle( 4, 3)}, {"table", rectangle( 3, 2)}, {"billboard", rectangle(11, 8)} }; const size_t n = sizeof a / sizeof a[0]; for (const thing *p = a; p < a + n; ++p) { cout << "--------------------------------------------\n" << "The area of the " << p->name << " is " << p->rect.area() << " square units.\n" "The perimeter of the " << p->name << " is " << p->rect.perimiter() << " units.\n" << "The diagonal of the " << p->name << " is " << p->rect.diagonal() << "units.\n"; } return EXIT_SUCCESS; }