I am being asked to use something other than all of those if/else statements. Any suggestions? I kind of stuck there. Also, I'm being asked to not initialized the arrays, and get the data straight from the file, but when I don't initialized the array, the program doesn't display the data. any suggestions?
here is my code:
//Bianelis Liriano
//LAB07b
//10/19/13
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Function prototype
void showData(string[], double [][4], int size);
int findCompanyIndex (string, string[], int size);
//global variable
int count = 0;
int main()
{
const int ROWS = 6; //constant for the rows
const int COLMN = 4; //contant for the columns
const int COMPANY = 6; //contant for the company names columns
string company_name; //string to get company name
int qtr_num = 0; //variable for quarters
string name[COMPANY] = { "ABC", "DEF", "GHI", "JKL", "MNO", "BL"};
double table [ROWS][COLMN] = {{40000, 50000, 60000, 70000},
{35000, 45000, 66000, 65000},
{25000, 26000, 27000, 28000},
{31000, 32000, 33000, 34000},
{42000, 43000, 44000, 45000},
{10000, 20000, 30000, 40000}};
//Open the file
ifstream inputFile("c:\\temp\\Data.txt");
//Read the data from the file
while (count < ROWS && inputFile >> table[ROWS][COLMN])
count++;
//Close the file
inputFile.close();
//Call showData to print to the screens both arrays
showData(name, table, COMPANY);
do
{
cout << endl;
do
{
findCompanyIndex(company_name, name, COMPANY);
}while (count == -1);
if (count >= 1)
{
do
{
cout << "Enter the quarter: ";
cin >> qtr_num;
if(count == 1)
{
if (qtr_num == 1)
{
cout << "The sales for the company ABC for quarter 1 is " << table[0][0];
cout << endl;
}
if (qtr_num == 2)
{
cout << "The sales for the company ABC for quarter 2 is " << table[0][1];
cout << endl;
}
if (qtr_num == 3)
{
cout << "The sales for the company ABC for quarter 3 is " << table[0][2];
cout << endl;
}
if (qtr_num == 4)
{
cout << "The sales for the company ABC for quarter 4 is " << table[0][3];
cout << endl;
}
}
else if(count == 2)
{
if (qtr_num == 1)
{
cout << "The sales for the company DEF for quarter 1 is " << table[1][0];
cout << endl;
}
if (qtr_num == 2)
{
cout << "The sales for the company DEF for quarter 2 is " << table[1][1];
cout << endl;
}
if (qtr_num == 3)
{
cout << "The sales for the company DEF for quarter 3 is " << table[1][2];
cout << endl;
}
if (qtr_num == 4)
{
cout << "The sales for the company DEF for quarter 4 is " << table[1][3];
cout << endl;
}
}
else if(count == 3)
{
if (qtr_num == 1)
{
cout << "The sales for the company GHI for quarter 1 is " << table[2][0];
cout << endl;
}
if (qtr_num == 2)
{
cout << "The sales for the company GHI for quarter 2 is " << table[2][1];
cout << endl;
}
if (qtr_num == 3)
{
cout << "The sales for the company GHI for quarter 3 is " << table[2][2];
cout << endl;
}
if (qtr_num == 4)
{
cout << "The sales for the company GHI for quarter 4 is " << table[2][3];
cout << endl;
}
}
else if(count == 4)
{
if (qtr_num == 1)
{
cout << "The sales for the company JKL for quarter 1 is " << table[3][0];
cout << endl;
}
if (qtr_num == 2)
{
cout << "The sales for the company JKL for quarter 2 is " << table[3][1];
cout << endl;
}
if (qtr_num == 3)
{
cout << "The sales for the company JKL for quarter 3 is " << table[3][2];
cout << endl;
}
if (qtr_num == 4)
{
cout << "The sales for the company JKL for quarter 4 is " << table[3][3];
cout << endl;
}
}
else if(count == 5)
{
if (qtr_num == 1)
{
cout << "The sales for the company MNO for quarter 1 is " << table[4][0];
cout << endl;
}
if (qtr_num == 2)
{
cout << "The sales for the company MNO for quarter 2 is " << table[4][1];
cout << endl;
}
if (qtr_num == 3)
{
cout << "The sales for the company MNO for quarter 3 is " << table[4][2];
cout << endl;
}
if (qtr_num == 4)
{
cout << "The sales for the company MNO for quarter 4 is " << table[4][3];
cout << endl;
}
}
else if(count == 6)
{
if (qtr_num == 1)
{
cout << "The sales for the company BL for quarter 1 is " << table[5][0];
cout << endl;
}
if (qtr_num == 2)
{
cout << "The sales for the company BL for quarter 2 is " << table[5][1];
cout << endl;
}
if (qtr_num == 3)
{
cout << "The sales for the company BL for quarter 3 is " << table[5][2];
cout << endl;
}
if (qtr_num == 4)
{
cout << "The sales for the company BL for quarter 4 is " << table[5][3];
cout << endl;
}
}
//***********************************************************************
// This funtion will print to the sreen the contents of the two arrays. *
//***********************************************************************
void showData(string comp [], double CABLE [][4], int size)
{
cout << "Here is the data from the file. \n";
for (int index = 0; index < 6; index++)
{
cout << "Company: ";
cout << comp[index] << " " ;
int x_axis = 1;
for (int x = 0; x < 4; x++)
{
cout << "QTR " << x_axis << ": ";
cout << CABLE [index][x] << " ";
x_axis++;
}
cout << endl;
}
return;
}
int findCompanyIndex (string company_name, string alpha[], int size)
{
do
{
cout << "Enter the name of the company: ";
cin >> company_name;
if (company_name != "Stop" || company_name != "STOP" || company_name != "stop")
{
if (company_name == alpha[0])
{
count = 1;
return count;
}
else if (company_name == alpha[1])
{
count = 2;
return count;
}
else if (company_name == alpha[2])
{
count = 3;
return count;
}
else if (company_name == alpha[3])
{
count = 4;
return count;
}
else if (company_name == alpha[4])
{
count = 5;
return count;
}
else if (company_name == alpha[5])
{
count = 6;
return count;
}
else if (company_name == "stop" || company_name == "Stop" || company_name == "STOP" )
{
count = 0;
return count;
}
else
{
cout << company_name << " is not a valid company name \n";
count = -1;
return count;
}
}
}while(count == -1);