I have an assignment to write a program that asks a user the year, model, and condition of a vehicle and display the price. We have to use a 3-D array to hold all of the values and a 2-D array to hold all of the names of the cars. The function I am having a problem with is the one that asks the user the year, model, and condition of the car. Say the person enters "Camry" for the model and "Used" for the condition, I need a way to assign a numeric value to a variable to represent the location of the price in the 3-D array. At first I was going to use a switch statement but visual studio kept telling me that you cant use a switch statement with a string. Here is the code I have so far, I'll post the whole thing but I pointed out where I was having the problem.
Code:
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cmath>
#include<cctype>
using namespace std;
char userprompt();
void getdata(ifstream& Readfile, float carvalues[100][17][4]);
void openfile(ifstream& Readfile);
float findcarvalue(char carnames[100][100], float carvalues[100][17][4]);
int main()
{
float carvalues[100][17][4];
char carnames[100][100]={"Altima", "Avalanche", "Camaro", "Camry", "CTS", "E350", "Escalade", "Explorer", "F150", "Fairlane", "S600", "Silverado", "Suburban", "Tacoma", "Tahoe", "Titan", "Tundra"};
float carvalue;
ifstream Readfile;
openfile(Readfile);
getdata(Readfile, carvalues);
switch(userprompt())
{
case'f':
case'F':
carvalue = findcarvalue(carnames, carvalues);
cout << endl << "The price of that vehicle is " <<carvalue;
break;
case'c':
case'C':
cout << "computeaverage";
break;
case's':
case'S':
cout << "showprices";
break;
case'h':
case'H':
cout << "cheapestcar";
break;
case'a':
case'A':
cout << "showallvalues";
break;
case'q':
case'Q':
break;
default:
cout << "Invalid Input!";
}
return 0;
}
void openfile(ifstream& Readfile)
{
string pathname;
cout << "Hello, Welcome to Kelly Green Book." << endl;
cout << "What is the name of the input file? " << endl;
cin >> pathname;
Readfile.open(pathname.c_str());
}
char userprompt()
{
char action;
cout << endl << "What would you like to do?" << endl
<< "F = Find a car value" << endl
<< "C = Compute average" << endl
<< "S = Show Prices" << endl
<< "H = Find the cheapest car" << endl
<< "A = Show all values" << endl
<< "Q = Quit" << endl;
cin >> action;
return action;
}
void getdata(ifstream& Readfile, float carvalues[100][17][4])
{
float price;
for (int year = 0; year < 7; year++)
{
for (int model = 0; model < 17; model++)
{
for (int condition = 0; condition < 4; condition++)
{
Readfile >> price;
carvalues[year][model][condition] = price;
}
}
}
}
float findcarvalue(char carnames[100][100], float carvalues[100][17][4])
{
float year, mlocation, carvalue, clocation;
char model, condition;
cout << endl << "Year: "; //Here is where I am having the problem.
cin >> year;
year = 2007 - year;
cout << endl << "Model: ";
cin >> model;
cout << endl << "Condition(Excellent/Fine/Good/Used): ";
cin >> condition;
return carvalue;
}
Any help would be great, Thanks.