Hmm, well, you made me download and compile it.
The problem is with your indentation.
On line 25, you declare and initialize a variable ("store1") in a scope local to the
if statement on line 24.
By the time the program reaches line 26, that local variable is destructed and destroyed --it no longer exists.
Then, down on lines 33 and 37 you try to use a variable named "store1", which never existed in the current scope (it did, for a moment, in a sub-scope block, but that doesn't help).
Between lines 14 and 15 add
producecomp store1, store2, store3;
and on lines 25, 27, and 29 you'll have to get rid of the variable instantiation and change the initialization to a non-static one.
1 2 3 4 5
|
{
store1.name = zone;
store1.store = aone;
store1.totalcost = xone*yone;
}
|
One thing of note: when you start using names like "foo1", "foo2", ..., and/or you start doing the same kind of thing over and over, that is a good candidate for either a loop or an array.
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
|
#include <iostream>
#include <limits>
#include <string>
using namespace std;
struct producecomp
{
string name;
string store;
float totalcost;
producecomp( string name = "", string store = "", float totalcost = 0.0 ):
name( name ), store( store ), totalcost( totalcost )
{ }
};
int main()
{
const int number_of_stores = 3;
string produce_name, store_name;
float cost_per_pound, number_of_pounds;
producecomp stores[ number_of_stores ];
cout << "Enter name of a specific produce: ";
getline( cin, produce_name );
for (int store_counter = 0; store_counter < number_of_stores; store_counter++)
{
cout << "\nEnter name of a store selling this produce: ";
getline( cin, store_name );
cout << "Enter cost per pound of this produce at that store: ";
cin >> cost_per_pound;
cout << "Enter number of pounds: ";
cin >> number_of_pounds;
cin.ignore( numeric_limits<streamsize>::max(), '\n' ); // get rid of remaining newline
stores[ store_counter ] = producecomp( produce_name, store_name, cost_per_pound * number_of_pounds );
}
cout << '\n';
for (int store_counter = 0; store_counter < number_of_stores; store_counter++)
{
cout << stores[ store_counter ].store
<< " sells "
<< stores[ store_counter ].name
<< " for "
<< stores[ store_counter ].totalcost
<< ".\n";
}
int best_value_index = 0;
for (int store_counter = 1; store_counter < number_of_stores; store_counter++)
{
if (stores[ store_counter ].totalcost < stores[ best_value_index ].totalcost)
best_value_index = store_counter;
}
cout << '\n'
<< stores[ best_value_index ].store
<< " is the best value for "
<< produce_name
<< ".\n";
// system("pause");
return 0;
}
|
Things to note:
The
producecomp object was given a constructor which can be used both as a default constructor and as an initialization constructor so that all its fields can be initialized at once like you were doing before.
I used variable names that make it easy to understand.
I used a constant to indicate the number of stores, instead of a magic number spread throughout the code.
I modified the user input stuff to account for the user pressing ENTER, and to permit the user to enter names of more than one word: "red grapes", "Intergalactic Food Mart".
I used a loop to find the store with the best value.
I used whitespace and indentation liberally, both in the program and when interacting with the user, to make things more readable.
Hope this helps.