arrays

this is my source code the program asks the user the two files they want to input one file has just prices in it so numbers like 20.12 and they are all on separate lines then the second file has names of parts like carb_cap also on new lines and these two files lines match each other like lets say the first line of the first file is a price the first line in the second file would be the name of a part that costs that much.

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

using namespace std;

int open_file (double price [], string part [], ifstream& in_stream);
double min (double price [], string part [], ifstream& in_stream);
double max (double price [], string part [], ifstream& in_stream);

int main () {
double price [7];
string part [7];
ifstream in_stream;
open_file (price, part, in_stream);
min (price, part, in_stream);
max (price, part, in_stream);
}
int open_file (double price [], string part [], ifstream& in_stream) {
string filename_prices, filename_parts;
cout << "Enter the name of the file with the prices" << endl;
cin >> filename_prices;
in_stream.open (filename_prices.c_str());
if (!in_stream.is_open()) {
cout << "that is not a file" << endl;
return 0;
}
for (int i = 0; i < 7; ++i) {
in_stream >> price [i];
}
cout << "Enter the name of the file with the part names" << endl;
cin >> filename_parts;
in_stream.open (filename_parts.c_str());
if (!in_stream.is_open()) {
cout << "that is not a file" << endl;
return 0;
}
for (int c = 0; c < 7; ++c) {
in_stream >> part [c];
}
}
double min (double price [], string part [], ifstream& in_stream) {
double min;
string min2;
int i = 0;
for (;i < 7; ++i) {
if (price[i] < min) {
min = price[i];
}
}
for (int c = 0; c < 7; ++c) {
min2 = part[i];
}
cout << min2 << " $" << fixed << setprecision (2) << min << endl;
}
double max (double price [], string part [], ifstream& in_stream) {
double max = 0;
string max2;
int i = 0;
for (;i < 7; ++i) {
if (price[i] > max) {
max = price[i];
}
}
for (int c = 0; c < 7; ++c) {
max2 = part[i];
}
cout << max2 << " $" << max << endl;
}


this is the output

Enter the name of the file with the prices
data1.txt
Enter the name of the file with the part names
data2.txt
$0.00
$3500.00

i typed in data1.txt and data2.txt the numbers below are suppose to be min price and the name of the part that cost that much and vice versa for the max but the max price was found but for some reason the min was not and it will not output the part names. if someone could help i would greatly appreciate it
bump
You've forgot to initialize the min and max variables before using them.
what do you mean

double max (double price [], string part [], ifstream& in_stream) {
double max = 0;
string max2;


double min (double price [], string part [], ifstream& in_stream) {
double min;
string min2;
Ok, max is not uninitialized but min is. What I mean is that you need to assign a value to min before comparing it inside the loop.
if (price[i] < min) {

The loops that you use to set the values of min2 and max2 are also not working correctly. They just loop through and assign each array element so when the loops stop the value will always be that of part[6]. Instead of using an extra loop you could assign to max2 in the same place you assign to max (same with min2).

If you don't want the min and max functions to return values you should change the return type to void.
i do want them to return values and strings but the strings wont appear
Topic archived. No new replies allowed.