Struct 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
struct producecomp //creates a structure to be called on in lines 26, 28, and 30
{ 
string name;
string store;
float totalcost;
};
int main()
{
    
    string zone, aone;
    int repeatval = 1;
    int xone, yone;
    cout << "Enter name of a specific produce: ";
    cin >> zone;
    do{
    cout << "Enter name of a store selling this produce: ";
    cin >> aone;
    cout << "Enter cost per pound of this produce at that store: ";
    cin >> xone;
    cout << "Enter number of pounds: ";
    cin >> yone;
    if(repeatval == 1)
    producecomp store1 = {zone, aone, (xone*yone)}; /*sets values in accordance w/ the struct (e.g. zone = the name variable in line 4 */
    else if(repeatval == 2)
    producecomp store2 = {zone, aone, (xone*yone)}; /*sets values in accordance w/ the struct (e.g. zone = the name variable in line 4 */
    else if(repeatval == 3)
    producecomp store3 = {zone, aone, (xone*yone)}; /*sets values in accordance w/ the struct (e.g. zone = the name variable in line 4 */
    repeatval++;
}while (repeatval <= 3);

    cout << store1.store << " sells " << store1.name << " for " << store1.totalcost << ".\n";
    cout << store2.store << " sells " << store2.name << " for " << store2.totalcost << ".\n";
    cout << store3.store << " sells " << store3.name << " for " << store3.totalcost << ".\n";
    if(store1.totalcost < store2.totalcost && store1.totalcost < store3.totalcost)
    cout << store1.store << " is the best value for " << zone << endl;
    else if(store2.totalcost < store1.totalcost && store2.totalcost < store3.totalcost)
    cout << store2.store << " is the best value for " << zone << endl;
    else if(store3.totalcost < store1.totalcost && store3.totalcost < store2.totalcost)
    cout << store3.store << " is the best value for " << zone << endl;
    system("pause");
    return 0;
}

When I run it, it says store1, store2, and store3 are undeclared. Does anybody have an idea as to why?
It's a chain-reaction kind of error.

store1 etc are undeclared because the type producecomp doesn't exist, because it is "incomplete".

It is incomplete because it depends on the string type, which doesn't exist because it is "incomplete".

The string type is incomplete because you didn't #include <string> , and so the compiler has no idea what a "string" is.

Hope this helps.
I did that, no dice. :/

I've done it w/ char arrays, did nothing to the result.

I hate my program.
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.
Last edited on
This is a problem of scope/visibility.

The variables store1, store2 and store3 are declared within the
do.. while loop brackets. They are therefore only available up to the closing bracket ( line 31 ).

They are NOT available after that.

To make them available the way you want you will have to declare them somewhere roundabout line 12 ( where you have declared the zone and other variables.)
Last edited on
Gotcha.
Topic archived. No new replies allowed.