📄 foodsurvey.cpp
字号:
// Exercise 14.11: FoodSurvey.cpp
// This program takes a survey of opinions on various foods.
#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required for parameterized stream manipulators
#include <string> // required to access string functions
using namespace std; // for accessing C++ standard library members
// function prototypes
void displayMenu( string [], int );
// function main begins program execution
int main()
{
// define variables
const int ARRAY_SIZE = 5; // stores number of food items
int menuChoice = 0; // stores item to rate
char input; // stores whether user likes/dislikes item
// string array for food choices
string foodChoices[ ARRAY_SIZE ] = { "", "Pizza", "Hot Dog",
"Fish Sticks", "Mystery Meat" };
// repeat until user enters 5 to exit
while ( menuChoice != ARRAY_SIZE )
{
displayMenu( foodChoices, ARRAY_SIZE); // display the menu
cin >> menuChoice; // store the user's selection
// verify that the user entered a valid value
if ( menuChoice < 1 || menuChoice > ARRAY_SIZE )
{
cout << "Error: Enter a value between 1 and 5." << endl;
}
// if the user selected a food item
else if ( menuChoice < ARRAY_SIZE )
{
// stores whether user entered a valid food rating
bool validResponse = false;
// repeat until the user enters a valid rating
while ( validResponse != true )
{
// prompt user for and input rating
cout << "Do you like or dislike "
<< foodChoices[ menuChoice ]
<< " (enter l or d )? ";
cin >> input; // store the rating
// record input in the display array
} // end inner while
} // end else if
// display the total ratings
displayArray( display, foodChoices, ARRAY_SIZE );
} // end outer while
return 0; // indicates successful termination
} // end function main
// display a menu of options
void displayMenu( string options[], int size )
{
int i; // counter variable in the for statement
cout << "\nSelect food item: " << endl;
// display each item
for ( i = 1; i < size; i++ )
{
cout << i << " - " << options[ i ] << endl;
} // end for
cout << i << " - Exit this application" << endl;
cout << "? ";
} // end function displayMenu
// display the total ratings
void displayArray( int display[][ 2 ], string foodChoices[],
int size )
{
// output header
cout << "\n" << setw( 13 ) << left << "Food"
<< setw( 5 ) << "Like" << "Dislike" << endl;
} // end function displayArray
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -