3D array problem

This is a discussion on 3D array problem within the C++ Programming forums, part of the General Programming Boards category; I have an assignment to write a program that asks a user the year, model, and condition of a vehicle ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    3D array problem

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're going to have to figure out what index to use between 0 and 17 or whatever, based on the name of the car, right? If you knew std::map, you wouldn't be asking the question, so I'm guessing you don't. In that case, I guess you can loop over the names in your array o' names -- if the input and carnames[i] compare equal, then that's your index.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  2. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 08:53 AM
  3. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 12:26 PM
  4. help w/ array problem
    By rhythm313 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2002, 12:12 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 07:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21