Create an electronic restaurant guide in C++. A restaurant\'s record is made up
ID: 3591013 • Letter: C
Question
Create an electronic restaurant guide in C++.
A restaurant's record is made up of the restaurant name, its cuisine, its price range, and its star rating. Create an array that can hold ten restaurant records. Load the array with ten records based on data you create, (e.g.: "Antonio's Restaurant", "Italian", "High", 5). You may opt to read this data from a file or to manually load the data within the program.
The remainder of the program should allow the user to search the array by cuisine type or ratings. The program should display all records (elements) which meet the user's criteria.
Hints: If you are reading string data that contains spaces from the console or a file, you will need to use getline to input the data. In the case of reading sequential strings from a file (e.g.: name, then cuisine type), you will need to use a delimiter to separate the fields of data. One last reminder: when combining getline and cin in the same program, it is often necessary to use the ignore() function to deal with carriage returns that are stuck in the buffer.
Explanation / Answer
#include<iostream>
#include<string>
#include<sstream>
#include<fstream>
using namespace std;
struct restaurant {
string name;
string cuisine;
string price_range;
int rating;
};
int main(){
restaurant list[10000];
string line;
string cuisine;
string range;
int rating;
int count = 0;
ifstream fin;
fin.open("rest.txt"); //Assuming data is stotred in rest.txtf
if (!fin){
cout << "Error opening file ";
return 0;
}
while(getline(fin,line)){
istringstream iss(line);
while (iss >> list[count].name >> list[count].cuisine >> list[count].price_range>> list[count].rating){ //Assuming name does not contin spaces
count++;
}
}
fin.close();
cout << "Enter cuisine type, price range and rating to search: ";
cin >> cuisine >> range >> rating;
for (int i=0; i<count; i++){
if (list[i].cuisine == cuisine && list[i].price_range == range && list[i].rating == rating){
cout << list[i].name << endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.