Arrays, homework problem

Hello! I'm a newbie, let me know if I post anything against etiquette.

My program keeps crashing the console.

Is there something wrong with arrays of strings?

it should ask for the number of stores to compare, and then store the names of up to ten stores in a name array, and the price of a certain item for each of those 10 stores in another price array. It should then print all stores and all prices. Then it will compare the prices and output the lowest price and what store it's from.

I've been trying different things for like 3 days on and off with this. This is my most current iteration.

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
  #include <iostream>
#include <string>

using namespace std;

int main(){
	
	int size, lowPrice;
	string lowPriceName;
	string storeNames[size];
	double storePrices[size];
	
	cout<<"Enter the number of stores (<= 10): ";
	cin>>size;
	
	cout<<"Enter computer store name: ";
	cin>>storeNames[0];
	lowPriceName=storeNames[0];
	cout<<"Enter Mac PC price: ";
	cin>>storePrices[0];
	lowPrice=storePrices[0];
		
	for(int i=1; i<size;i++)
	{
		cout<<"Enter computer store name: ";
		cin>>storeNames[i];
		cout<<"Enter Mac PC price: ";
		cin>>storePrices[i];	
			if (storePrices[i]<lowPrice)
			{
				lowPrice=storePrices[i];
				lowPriceName=storeNames[i];
			}
	}
	
	for(int i=0; i<size;i++)
	{			
	cout<<"Store Name: "<<storeNames[i]<<endl;
	cout<<"Mac PC price: "<<storePrices[i]<<endl;
	}
	
	cout<<"Low Mac PC price at store "<<lowPriceName<<". Low price = $ "<<lowPrice;
	
	return 0;
}
Last edited on
The variable size is not initialised, it contains garbage, it cannot be used to specify the size of the array.

The array size should be a constant which is known at compile time.
Something like this perhaps:
1
2
3
	const int SIZE = 10;
	string storeNames[SIZE];
	double storePrices[SIZE];


You will need to use some other variable for the user-input, SIZE cannot be changed when the program is executing.
Thank you!

I moved the array definitions below the cin statement and it works. important lesson on stupid mistakes...
size is a variable. This means it can change in value. The size of an array must be constant and must be defined before the program can compile. Since you know you only want an array of 10 or less. Go ahead and get your array at max size like this
1
2
string storeNames[10];
 
I moved the array definitions below the cin statement and it works. important lesson on stupid mistakes...

Don't do that.

Of course you can place the definitions where you like, so long as you make the size a CONSTANT.

What is happening here is that you are relying on a non-standard compiler behaviour. That's the wrong way to go about learning C++, because it means if you use a different compiler, your code will not work. Your aim should be to learn how to write code that will work on any compiler which complies with the C++ standard.
I agree with Chervil. Doing what you said does not compile on my machine.
Topic archived. No new replies allowed.