Problems with error checking

I begin my very post with what I'm assuming is a very silly question. I'm working on an assignment for school, and while the program itself runs great, I can't seem to figure out where to properly code the error checking.

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
#include<iostream>
using namespace std;

int main()
{
    int sales[6], store, asteriskcount;
    
    
    for ( store = 1; store <=5; store++)
    {
        cout <<"Enter today's sales for store " << store << ": " << endl;
        cin >> sales[store];
             
      
    cout << "\nSales Report: " << endl;
    
        for ( store = 1; store <=5; store++ )
    {
        cout <<" \nStore " << store  <<":" << endl;
        asteriskcount = sales[store] / 100;
        
        
        
        for ( int numOfAsterics = 0; numOfAsterics < asteriskcount; numOfAsterics++)
        {
                       
        cout << '*';
        }
    
    }
    
    return 0;
}


What I'm trying to do is if they enter a negative number for a sale, it gives them an error and asks them to re-enter it for that particular store. Basically, I can't figure out where to put it without it being looped by default, and I'm hoping someone could give me a push in the right direction.
closed account (zb0S216C)
The most logical place is within the loop, immediately after requesting input, like this:

1
2
3
4
5
6
7
8
9
for ( store = 1; store <=5; store++)
{
    cout <<"Enter today's sales for store " << store << ": " << endl;
    cin >> sales[store];
    if(sales[store] < 0)
    {
        // Too low of a value...
    }
}

Wazzak
1
2
3
4
5
6
7
8
9
10
11
12
for ( store = 1; store <=5; store++)
    {
        cout <<"Enter today's sales for store " << store << ": " << endl;
        cin >> sales[store];
             
        if (sales[store] < 0)
        {
            cout << "Please enter a sales amount greater than 0.\n";
            
            
        }
        

I've encountered another issue, that I'm unsure how to counter. Say I enter -90, the error pops, but it doesn't pause for a new input. instead, it assumes I've entered 0 for the remaining four. I also just checked that with a positive number, it does the same. It put the correct amount of *, but it assumes 0 for the remaining.

1
2
3
4
5
6
7
8
9
10
11
12
for ( store = 1; store <=5; store++)
    {
        cout <<"Enter today's sales for store " << store << ": " << endl;
        cin >> sales[store];
             
        if (sales[store] < 0)
        {
            cout << "Please enter a sales amount greater than 0.\n";
            
            
        }
        continue;

now continue allows me to continue the loop, the graph does not display. If I enter a negative number, it displays the error, but continues on to the next request for input. I apologize if I'm being simple.
Instead of
1
2
3
4
if (sales[store] < 0)
{
   cout << "Please enter a sales amount greater than 0.\n";
}


I'd imagine you'd want something like
1
2
3
4
5
while (sales[store] < 0)
{
   cout << "Please enter a sales amount greater than 0.\n";
   cin >> sales[store];
}
You need to think more clearly about what you want to take place when the user enters a negative value. I suspect that what you are after would look something like this:

Enter today's sales for store 1: -40
Error - Please enter a positive value
Enter today's sales for store 1: 50
Enter today's sales for store 2: 30
..... and so on

As an outline, the process might look like this:

1
2
3
4
5
6
7
8
9
I. Get values
    A. For each store
        1. Get sales
        2. If sales is negative, report error
        3. Repeat until sales is not negative
II.  Show graph
    A. Output title
    B. For each store
        1. Output "bar"


Whenever a process involves repeating a step, you're looking at a loop,
and you have three choices: for, while, or do. I'll let you look into those to determine which one will work best in this case. There's also a sophisticated change you could make in the for loop, but I don't recommend it.

As long as you are error checking, ask yourself why you are checking for negative values. Is there any other input that can cause undesired results? How should those be handled? Is forcing the user to reenter invalid data the only option?

Topic archived. No new replies allowed.