mercredi 29 avril 2015

Trying to display console art by using array with biggest value

My program is supposed to take user input of names and numerical values and then display it with console art (asterisks) It saves it all to a text file and can be reused.

We have to find the index of the biggest array element and then base our console art around that. For instance, if someone has 8000 sales, then they will have (at most possible) 50 asterisks in order to not flood the window. Then we have to evaluate the other array elements by using division (smaller element/bigger element and times that by 50)

We have a function that can find the index of the array with the biggest element so that's done. But we're having logic problems with the output of the display.

int main()

double sales[100];
string names[100];
string fileName = "";
string choice = "";
int indexOfLargest = 0;

//intro
cout << "Welcome to the sales Record Program!" << endl;
askForFileName(fileName);

do{
    displayMenu(choice);
    if (choice == "1")
    {
        indexOfLargest = findLargest(sales, fileName);
        enterInfo(fileName, names, sales);
    }
    else if (choice == "2")
    {
        indexOfLargest = findLargest(sales, fileName);
        displayInfo(fileName, indexOfLargest);
    }
}while (choice != "x");
cout << "Goodbye!" << endl;

Now my code that finds the index number:

int findLargest(double sales[], string fileName) 

int sz = 0;
string names;
int indexOfLargest = 0;
ifstream fin(fileName.c_str());
if (fin)
{
    cout << "file opened" << endl;
    while (isalnum(fin.peek()))
    {
        getline(fin, names);
        fin >> sales[sz];
        fin.ignore(5, '\n');
        sz++;
    }
    cout << "The size is " << sz << endl;
    //size = 4
    if (sz == 0) return -1;  // no data means no highest index
    for (int i = 0; i < sz; i++)
    {
        if (sales[i] > sales[indexOfLargest])
            indexOfLargest = i;
    }
    cout << "Index: " << indexOfLargest;
    fin.close();
}

return indexOfLargest;

And now the function that I am currently having problems with:

void displayInfo(string fileName, int indexOfLargest)

string names;
double sales = 0;
double bigSales = 0;
int counter = 0;
ifstream fin(fileName.c_str());
if (fin)
{
    cout << "file opened" << endl;
    while (isalnum(fin.peek()))
    {
        getline(fin, names);
        fin >> sales;
        fin.ignore(5, '\n');

        if (counter == indexOfLargest)
        {
            cout << setw(10) << left << names << setw(10) << left;
            for (int i = 1; i < 50; i++)
            {
                cout << "first" << endl;
                cout << "*";
                sales = bigSales;
            }
            counter++;
        }
        else
        {
            cout << setw(10) << left << names << setw(10) << left;
            for (int i = 0; i < ((sales/bigSales) * 50); i++)
            {
                cout << "second" << endl;
                cout << "*";
            }
            counter++;
        }

    }
}
return;

Other notes: We are using parallel arrays, text files, and whatever else I'll remember after I post this.

We know the problems with my logic on the last function but we don't know where to go. Say the first element in the array isn't the biggest one, then it will loops through the "else" forever because bigSales hasn't been changed from 0, so it will be an error * 50. I'm not sure if we should sort it (really not sure how to do it either) so we're looking for an alternative. (Yes, we know the last code is messy)

Aucun commentaire:

Enregistrer un commentaire