Hello! I'm trying to read from a .csv Excel file into my code to be displayed in a cout. Currently in my classes we haven't been taught about vectors, classes, templates or the like, so I'm basically trying to do this with only a struct array. Currently my code looks like this:
/*
This program will enable users to find places to eat in Barcelona by reading data from a ".csv" file.
The program has 5 options depending on the user's choice.
1. This program will show all restaurants in the database in a human readable format sorted alphabetically by Name
2. This program will show all restaurants in the database in a human readable format sorted by Price and Rating
3. This program will find a restaurant by Name using a binary search
4. This program will read a price range, then find the restaurant with the highest Rating at the given price range.
For example, if the userinputs “$$” you should find restaurant with a name “Viana”.
5. Exit the loop
*/
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace std;
constint SIZE = 100;
int readRestaurants();
void printRestaurants();
void sortRestaurantsByPriceRangeRating();
void sortRestaurantsByName();
int binarySearchName();
int findBestRestaurantByPrice();
int main(){
struct Restaurant{
string name;
double rating;
char priceRange;
int reviewNumber;
};
int choice = 0;
Restaurant database[SIZE];
do{
cout << "Please select your desired option" << endl << endl;
cout << "1. Display restaurants by Name" << endl;
cout << "2. Display restaurants by Price and Rating" << endl;
cout << "3. Find a restaurant" << endl;
cout << "4. Find the best restaurant by Price" << endl;
cout << "5. Exit the application" << endl;
cout << "Please input 1, 2, 3, 4, or 5: " << endl;
cin >> choice;
while(choice>5 || choice<1){
cout << "That option is not available. Please input a valid choice" << endl <<
cout << "Please input 1, 2, 3, 4, or 5: " << endl;
cin >> choice;
}
while(choice<=5 && choice >=1){
switch(choice){
case 1: sortRestaurantsByName();
case 2: sortRestaurantsByPriceRangeRating();
case 3: binarySearchName();
case 4: findBestRestaurantByPrice();
case 5: cout << "Thank you for using the Trip Advisor program. Goodbye!";
}
}
}while (choice <=4 && choice >=1);
return 0;
}
int readRestaurants(){
ifstream data;
data.open("restaurants.csv");
if (!data)
{
cout << "ERROR - File failed to open. make sure that "
<< "the input file and this file are in the"
<< "same directory" << endl;
return -1;
}
}
}
}
void printRestaurants(){
}
void sortRestaurantsByPriceRangeRating(){
}
void sortRestaurantsByName(){
}
int binarySearchName(){
}
int findBestRestaurantByPrice(){
}
As you can see I have the basic skeleton of the code done and the menu do-while switch completed. We aren't allowed to have variables global unless they're constants. Any help on this would be much appreciated!
Oops! I forgot the csv content:
Name Rating Price Range Number of Reviews
Uma 3.5 $$$$ 792
Viana 5 $$ 2707
Blavis 4.8 $$$ 643
My Restaurant 4.2 $$ 159
Bodega Biarritz 5 $ 1078
Santa Rita Experience 3.7 $$$$ 329
Rudi of Pirate Cooking 3.2 $$ 110
Chaka Khan 5 $ 479
Spoonik Restaurant 4 $$$$ 408
The Box 4.5 $ 834
For a CSV file, there seems to be a complete lack of commas.
Are there other delimiters in the file which don't show up as posted, like say for example TAB characters?
The biggest problem here is figuring out where the end of the restaurant name is and where the rating begins.
Your struct needs to be global, and you need to pass parameters.
You only have a single char for priceRange, but the file contains $ to $$$$.
Are you going to count the $, and store only a number from 1 to 4 ?
In which case, priceRange would be better stored as an integer.