I was wondering if there was a better way for me to have addressed a user puttin in a value that was not valid. I've got it working.. just wondering if it was the most efficient way.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int store1, // sales
store2,
store3,
store4,
store5,
star; // star variable
// Input sales figures
cout << "Enter today's sales for store 1: ";
cin >> store1;
// Validate the input
while (store1 <= 0)
{
//Explain the error
cout << "Sales number must be greater than 0 \n";
//Input sales figures again
cout << "Enter today's sales for store 1: ";
cin >> store1;
}
cout << "Enter today's sales for store 2: ";
cin >> store2;
// Validate the input
while (store2 <= 0)
{
//Explain the error
cout << "Sales number must be greater than 0 \n ";
//Input sales figures again
cout << "Enter today's sales for store 2: ";
cin >> store2;
}
cout << "Enter today's sales for store 3: ";
cin >> store3;
// Validate the input
while (store3 <= 0)
{
//Explain the error
cout << "Sales number must be greater than 0 \n";
//Input sales figures again
cout << "Enter today's sales for store 3: ";
cin >> store3;
}
cout << "Enter today's sales for store 4: ";
cin >> store4;
// Validate the input
while (store4 <= 0)
{
//Explain the error
cout << "Sales number must be greater than 0 \n ";
//Input sales figures again
cout << "Enter today's sales for store 4: ";
cin >> store4;
}
cout << "Enter today's sales for store 5: ";
cin >> store5;
// Validate the input
while (store5 <= 0)
{
//Explain the error
cout << "Sales number must be greater than 0 \n";
//Input sales figures again
cout << "Enter today's sales for store 5: ";
cin >> store5;
}
// Sales Bar Chart
cout << "\n\nSALES BAR CHART\n (Each * = $100)\n\n";
cout << "Store 1: ";
for (star = 1; star <= store1/100; star++)
cout << '*';
cout << endl;
cout << "Store 2: ";
for (star = 1; star <= store2/100; star++)
cout << '*';
cout << endl;
cout << "Store 3: ";
for (star = 1; star <= store3/100; star++)
cout << '*';
cout << endl;
cout << "Store 4: ";
for (star = 1; star <= store4/100; star++)
cout << '*';
cout << endl;
int stores[5]={0}; //Create an array with 5 elements and initialize all elements to 0
unsigned i; //Loop variable
for (i=0;i<5;i++) //Arrays start at 0 and end at number of elements-1. So the stores array has elements stores[0] .. stores[4]
{
cout << "Please enter today's sales for store " << i+1 << ": "; //i+1 because otherwise it'd say store 0..4, which looks wrong.
cin >> stores[i];
while (stores[i] <= 0)
{
cout << "Sales number must be greater than 0\n"
<< "Please enter today's sales for store " << i+1 << ": ";
if (!cin.good()) // If we got input that we totally couldn't handle
{
cin.clear(); // Clear the error flags on cin
cin.ignore(INT_MAX, '\n'); // And flush out the bad information they typed.
}
cin >> stores[i];
}
}
Hopefully you can learn from this and adapt it to do other things as well.
Edit: I also added a helpful little piece of code that will let your program not break if it gets completely off the wall input like "aoifaefnozefj" instead of a number like it wants. It's useful to know about if you're dealing with input streams like cin.