Trying to find biggest number in partially filled, parallel arrays

So I have a partially filled array and I'm trying to find the biggest number. All of the tools my teacher gave us only works with completely filled arrays, so I'm lost here.

My arrays are parallel. One is a double array (but keeps outputting int when being viewed - I'll ask about that later) and the other is a string.

We have to display the information in console art. That's why I have to find the biggest, so we can generate a screen that wont be too big or too small. The largest number will be the biggest, and the rest of them are going to be calculated based on percentages.


Here's my function:
1
2
3
4
5
6
7
8
9
10
11
  int findLargest(double sales[])
{
    int indexOfLargest = 0;
    
    for (int i = 0; i < 100; i++)
    {
        if (sales[i] > sales[indexOfLargest])
            indexOfLargest = i;
    }
    return indexOfLargest;
}


Here's what part of main looks like:
1
2
3
4
5
6
7
    double sales[100];
    string names[100];
    
    int elementNumber = 0;
    
    elementNumber = findLargest(sales);
    cout << "largest element number is " << elementNumber << endl;


When I run this code to test it, it outputs that the array with the largest number has the index of 78, even though I have one entry so it should be 0. Terribly confused honestly, any COMPREHENSIVE resources would be great.
Last edited on
for (int i = 0; i < 100; i++)

Think about why the condition for the loop is i < 100.
I've posted this to stack overflow and have gotten answers about that but that doesn't answer my question.
Here's my full code right now
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

void askForFileName(string &fileName);
void displayMenu(string &choice);
void enterInfo(string fileName, string names[], double sales[]);
void displayInfo(string fileName);
int findLargest(double sales[], int sz, string fileName);

int main()
{
    //declare variables
    double sales[100];
    string names[100];
    string fileName = "";
    string choice = "";
    int elementNumber = 0;
    int sz = 0;
    
    //intro
    cout << "Welcome to the sales Record Program!" << endl;
    askForFileName(fileName);
    cout << fileName << endl;
    
    //this was some testing
    elementNumber = findLargest(sales, sz, fileName);
    cout << "largest element number is " << elementNumber << endl;
    
    do{
        displayMenu(choice);
        if (choice == "1")
        {
            enterInfo(fileName, names, sales);
        }
        else if (choice == "2")
        {
            displayInfo(fileName);
        }
    }while (choice != "x");
    cout << "Goodbye!" << endl;
}

//this one works just fine
void askForFileName(string &fileName)
{
    cout << "Please enter the filename: ";
    getline(cin, fileName);
    ifstream fin(fileName.c_str(), ofstream::app);
    if (fin)
    {
        cout << "file opened!" << endl;
        fin.close();
    }
    
    
    return;
}
//this one works fine too
void displayMenu(string &choice)
{
    cout << "\nWould you like to:" << endl;
    cout << " 1 - add a sales record" << endl;
    cout << " 2 - view the sales report" << endl;
    cout << " x - exit" << endl;
    cout << " > ";
    getline(cin, choice);
    return;
}

//so this one 
void enterInfo(string fileName, string names[], double sales[])
{    
    
    //declare variables
    string newName = "";
    double newSales = 0;
    int counter = 0;
    
    cout << "Enter the name: ";
    getline(cin, newName);
    cout << "\nEnter the sales: ";
    cin >> newSales;
    cin.clear();
    cin.ignore(100, '\n');
    
    //read file
    ifstream fin(fileName.c_str(), ofstream::app);
    if (fin)
    {
        while (isalnum(fin.peek()) && counter < 100)
        {
            getline(fin, names[counter]);
            fin >> sales[counter];
            fin.ignore(100, '\n');
            if (names[counter] != newName)
                counter++;
            
        }
        fin.close();
        
    }
    //write to file
    ofstream fout(fileName.c_str(), ofstream::app);
    if (fout)
    {
        for (int i = 0; i < counter; i++)
        {
            fout << names[i] << "\n";
            fout << sales[i] << "\n";            
        }
        fout << newName << "\n";
        fout << newSales << "\n";
        fout.close();
        cout << "success!\n";
    }
    else
        cout << "failure.\n";
    
    return;
}

//this one works? but it's very weird
void displayInfo(string fileName)
{    
    string names;
    double sales = 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');
            cout << setw(10) << names << setw(10) << sales <<  " *asterisks* " << endl;
        }
    }
    return;
}

//currently editing this bit
//cannot figure out a way to find the biggest number. I thought it was working but I guess not.
int findLargest(double sales[], int sz, string fileName) 
{
    int indexOfLargest = 0;
    ifstream fin(fileName.c_str());
    if (fin)
    {
        cout << "file opened" << endl;
        while (isalnum(fin.peek()))
        {
            if (sz == 0) return -1;  // no data means no highest index
            for (int i = 1; i < sz; i++)
                if (sales[i] > sales[indexOfLargest])
                    indexOfLargest = i;
        }
    }
    
    return indexOfLargest;
}
You haven't actually asked a question. That piece of code is the reason for the problem you described.
How do I find the biggest number in an array?
Well, you got this part right:
160
161
162
            for (int i = 1; i < sz; i++)
                if (sales[i] > sales[indexOfLargest])
                    indexOfLargest = i;

The problem is that you didn't actually fill the arrays with any info yet. And that should probably be outside the while loop.
Forgot to mention that this is a text file and the user can input data. I set the array to fill 100 but I'm not expecting them to use that much.

I've created a counter (size/sz) and it is returning the correct amount of data entered by the user, but the function still wont return the largest index number.
It knows that there's 4 names and 4 numbers

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

void askForFileName(string &fileName);
void displayMenu(string &choice);
void enterInfo(string fileName, string names[], double sales[]);
void displayInfo(string fileName);
int findLargest(double sales[], int &sz, string fileName);

int main()
{
    //declare variables
    double sales[100];
    string names[100];
    string fileName = "";
    string choice = "";
    int size = 0;
    int indexOfLargest = 0;
    
    
    //intro
    cout << "Welcome to the sales Record Program!" << endl;
    askForFileName(fileName);
        
    indexOfLargest = findLargest(sales, size, fileName);

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

//this one works just fine
void askForFileName(string &fileName)
{
    cout << "Please enter the filename: ";
    getline(cin, fileName);
    ifstream fin(fileName.c_str(), ofstream::app);
    if (fin)
    {
        cout << "File opened." << endl;
        fin.close();
    }
    
    
    return;
}
//this one works fine too
void displayMenu(string &choice)
{
    cout << "\nWould you like to:" << endl;
    cout << " 1 - add a sales record" << endl;
    cout << " 2 - view the sales report" << endl;
    cout << " x - exit" << endl;
    cout << " > ";
    getline(cin, choice);
    return;
}

//so is this one 
void enterInfo(string fileName, string names[], double sales[])
{    
    
    //declare variables
    string newName = "";
    double newSales = 0;
    int counter = 0;
    
    cout << "Enter the name: ";
    getline(cin, newName);
    cout << "\nEnter the sales: ";
    cin >> newSales;
    cin.clear();
    cin.ignore(100, '\n');
    
    //read file
    ifstream fin(fileName.c_str(), ofstream::app);
    if (fin)
    {
        while (isalnum(fin.peek()) && counter < 100)
        {
            getline(fin, names[counter]);
            fin >> sales[counter];
            fin.ignore(100, '\n');
            if (names[counter] != newName)
                counter++;
            
        }
        fin.close();
        
    }
    //write to file
    ofstream fout(fileName.c_str(), ofstream::app);
    if (fout)
    {
        for (int i = 0; i < counter; i++)
        {
            fout << names[i] << "\n";
            fout << sales[i] << "\n";
        }
        fout << newName << "\n";
        fout << newSales << "\n";
        fout.close();
        cout << "success!\n";
    }
    else
        cout << "failure.\n";
    
    return;
}

//this one works? but it's very weird
//practice one!
void displayInfo(string fileName)
{    
    string names;
    double sales = 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');
            cout << setw(10) << names << setw(10) << sales <<  " *asterisks* " << endl;
        }
    }
    return;
}

//currently editing this bit
//cannot figure out a way to find the biggest number. I thought it was working but I guess not.
int findLargest(double sales[], int &sz, string fileName) 
{
    double numbers = 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 >> numbers;
            fin.ignore(5, '\n');
            sz++;
        }
        //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 << indexOfLargest;
        fin.close();
    }
    
    return indexOfLargest;
}
Last edited on
From what I can see, you're calling your findLargest function before you input any data. How can you expect it to return anything other than 0 when you haven't entered anything yet?
this is a text file and the user can input and save data

Here's my snippets to make it easier to read
main:
1
2
3
4
5
6
7
8
9
10
11
12
13
//declare variables
    double sales[100];
    string names[100];
    string fileName = "";
    int size = 0;
    int indexOfLargest = 0;
    
    
    //intro
    cout << "Welcome to the sales Record Program!" << endl;
    askForFileName(fileName);
        
    indexOfLargest = findLargest(sales, size, fileName);

now the function:
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[], int &sz, string fileName) 
{
    double numbers = 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 >> numbers;
            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;
}


My textfile looks like:
Bob
2000
Billy
9000
Last edited on
So you mean that the file already has data in it before you start the program?

Anyway, there is still a problem:

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
int findLargest(double sales[], int &sz, string fileName) 
{
    //right now sales is "empty" because you haven't input anything into it
    double numbers = 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 >> numbers; //you store the data in "numbers"
            fin.ignore(5, '\n');
            sz++;
        }
        //size = 4
        //right now sales is still "empty" because you haven't stored anything in the array
        if (sz == 0) return -1;  // no data means no highest index
        for (int i = 0; i < sz; i++)
            if (sales[i] > sales[indexOfLargest]) //you are comparing garbage values right here
                indexOfLargest = i;
        cout << indexOfLargest;
        fin.close();
    }
    
    return indexOfLargest;
}
Yeah sorry I couldn't explain it very well. It already has data when I'm running it.

When I used "sales" in place of "numbers", I got an error. It says "invalid operands to binary expression." I just made a different variable instead and left it. How can I fix that issue?

More specifically - "invalid operands to binary expression ('ifstream' (aka 'basic_ifstream<char>' and 'double')"
Last edited on
You probably made a mistake when you changed it.

fin >> sales[sz];
Added that and now my program just stops after I enter the filename (the function comes right after that) The menu didn't appear like it was supposed to. Darn code.

So I entered a new filename, and then it continued to the menu like normal. But when I entered a filename that previously existed, it didn't continue.

I'm gonna talk this out. So the function starts out as a while loop and peeks at the next line to see if anythings there. sz is incrementing once per loop, so this should make sales[sz] go up each time. But it's stopping there.


Never mind. It was something stupid with Xcode. Now I've got that working! Yay! Now I'm going to try to implement the part that I'm actually using it with. I hope this bit goes more smoothly.
Thank you for your help!
Last edited on
Topic archived. No new replies allowed.