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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
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. The user can reopen the text file to view the data they have already entered.
We know the problems with the 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 but we just want to get it working.