Question about variables in loops for an assignment

Hello everyone. I have an assignment where I need to get input from a store owner that owns 5 stores. The owner is meant to enter the sales for today, from each store. I am then to output a bar graph, the bars being asterisks, comparing the sales amounts. The output is meant to look something like this:

Enter sales for store 1: 100
Enter sales for store 2: 200
Enter sales for store 3: 300
Enter sales for store 4: 400
Enter sales for store 5: 500

Bar Graph
* = $100
_________
Store 1: *
Store 2: **
Store 3: ***
Store 4: ****
Store 5: *****

Now, I could write a program to do that, without using loops. I know though that there must be a way to do this within a loop. So far, I have asked for the sales amounts individually, not in a loop, and stored them in different variables (sales1, sales2, etc.). My question stems from this wall I've hit, where I'm not sure how to proceed. Is there a way, in a loop, to advance a variable to another variable? So, for example, I have sales1, sales2, sales3, etc. In a loop, can I have the loop get a sales amount for sales1, then loop around and instead of asking for sales1, ask for sales2, and do that until it stores something in each separate variable?
You would make the loop very similar to how you would ask for each individually.
1
2
3
4
5
6
7
8
9
10
11
 
//instead of this
std::cout << "Enter sales for store 1\n";
std::cout << "Enter sales for store 2\n";
std::cout << "Enter sales for store 3\n";
std::cout << "Enter sales for store 4\n";
std::cout << "Enter sales for store 5\n";

//create a loop that goes for the numbers 1 to 5 a for loop would probably be easiest but the other loops will work as well.
//use std::cout << "Enter sales for store " << count << "\n";
//count will be the way you keep track of the number 


You could store that values that you read into a container of your choosing and then display them as needed. You could also use named variables for the data. Since you need to do some sort of validation to create the graph, you need to store the input somewhere.
Okay, I somewhat understand what you're saying. How would I go about storing the input in different variables, if I was receiving the input within a loop?
Have you already studied arrays or std::vector?
I've never seen std::vector, but we did just talk about character arrays, although it was a bit confusing.
This something arrays and a for loop are built for.

Instead of naming every variable individually like sales1, sales2, sales3, sales4,...
You can make an array and address each variable with an index: sales[0], sales[1], sales[2], sales[3]...

The advantage of this is clear in a for loop: for (int i=0; i<5; i++)
because now you can address each sales variable with sales[i] inside the loop.
This answers your question:
Is there a way, in a loop, to advance a variable to another variable?


Thank you Ihatov, however I don't recognize the [i] portion of the array, or in the variable names. What I have seen so far is something like: char name[30], which I know is a character array that can hold 29 characters and the 30th is the null character. Is the [i] portion of sales[i] incrementing with each iteration of the loop? And if so, is that linking to the index you mentioned? How does an index work? This is all pretty new to me. I'll do some independent research right now, but it doesn't hurt to get all the info I can.
It looks like you haven't studied arrays above basic declaration of character arrays so I'll give you a crash course.

First of all you can make an array out of anything you want, not just char.
For example you can make an int array with 10 elements like this: int array[10];

Next, you can change individual elements using array[<index>] = value.
array[0] = 5;
array[1] = 10;
array[2] = 15;
array[3] = 20;
array[4] = 25;
array[5] = 30;
array[6] = 35;
array[7] = 40;
array[8] = 45;
array[9] = 50;


The point of arrays is that you can contract above by using a loop like this:
for (int i=0; i<10; ++i)
array[i] = 5 * (i + 1);


You can even load a number from std::cin into an element like this:
std::cin >> array[i];

And of course you can print out an element too.
std::cout << array[i];

And you can read all elements or print all elements from an array using a for loop which I leave you as an exercise.

There is practically no difference between an array and individually naming every variable array1 array2 array3...
Except that you get massive advantage by being able to put a formula as the index of a variable inside [] brackets.
Of course, everything so far doesn't only apply to int arrays, it applies to any kind of arrays (even char arrays).

You can read more here: http://www.cplusplus.com/doc/tutorial/arrays/

A std::vector is nothing more than an array that you can easily manipulate, put elements into, delete elements from, etc.
It's a bit hard to change the size of an array for example, but with std::vectors it's very easy.
Last edited on
Thank you for the detailed explanation. I did find that tutorial page while I was looking around and was messing around with how I thought it works. This is what I came up with:
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;

// Constants for bar graph explanation
const string TITLE = "SALES BAR CHART";
const string KEY = "(Each * = $100";
const string UNDERLINE = "_________________";

int main() {
	
	// Sales amounts for each of the five stores, in an index
	int sales[5];

	for (int i = 1; i < 6; i++) {
		cout << "Please enter today's sales for store " << i << ": ";
		cin >> sales[i];
	}

	cout << sales[1] << endl << sales[2] << endl << sales[3] << endl << sales[4] << endl << sales[5] << endl << endl;
	
	
	return 0;
} // End main 


I think I did it right, and the program I wrote works, but I get a weird error message from MS Visual Studio saying a runtime check failure # 2 - S. Am I doing something very wrong here?
The problem is that array indicies start from 0 (inclusive) and end in n - 1 (inclusive) where n is the size of the array.

For example if you have int array[5] (here, n is 5) then the elements are:
array[0]
array[1]
array[2]
array[3]
array[4]


Like you see, the first element has the index zero, and the last element has the index 4 (which n - 1).

Hence your for loop should go from 0 to 5 (with 0 and without 5. Without, because it says < not <=):
for (int i = 0; i < 5; i++)

Also note that like I said, you can put printing in a for too:
for (int i = 0; i < 5; i++)
cout << sales[i] << endl;


In C++ you mustn't ever access an element with:
* a negative index
* with an index equal or larger than the size of the array.
This will always crash your program.
In your code, you've accessed array[5] but the size of the array is 5 and that's why you got an error.
Last edited on
Okay, I understand more now, thank you for all of the explanations! I'm gonna give this another try.
Topic archived. No new replies allowed.