Thursday, January 3, 2013

Creating a simple store system in C++ using arrays

I explained you the basics of arrays in C++ with some examples to make you understand them clearly in the couple of previous posts. I'll show a little complex example here where the knowledge of arrays and control structures of C++ are used. If you are a beginner, please go through the previous posts of this blog and com back to do this exercise.

Let me give you the homework first. Following is a set of instructions to build this program. Go through it and then draw a little sketch or a flow chart as you wish to narrow down the big picture. After that understand the inputs and outputs. Finally come up with the source code of the below shown target.



Store a list of prices of items 1 to 100
You need to do the following to the List

1.  Add Item
2.  Find Price

Initialize an array of 100 elements to -1

1. Add Item -  Enter the item no ( 1 – 100 ) and the price and assign the array element to the price

2.  Find Price - To find an item , input the item no, recall the price value

Sample Output

MENU

1.  Store Item Price
2.  Get item Price

Enter your option :   1

Enter Item No : 5
Enter Price : 78.00

Do you want to continue (y/n): y

1.  Store Item Price
2.  Get item Price

Enter your option :   1

Enter Item No : 45
Enter Price : 230.00

Do you want to continue (y/n): y

1.  Store Item Price
2.  Get item Price

Enter your option :   2

Enter Item No : 5
The Price of Item No 5 is Rs. 78.00

Do you want to continue (y/n): y

1.  Store Item Price
2.  Get item Price

Enter your option :   2

Enter Item No : 4
Price is not set

Do you want to continue (y/n): y

1.  Store Item Price
2.  Get item Price

Enter your option :   2

Enter Item NO : 200
Invalid Item No

Do you want to continue (y/n): n

“Good Bye and have a nice day ……!”


Hints :
You can use a float array to store the items.  Initialize the array to -1

Assume that itemno10 is stored in index 10 in the items array.

You need to validate the itemno so that it is between the range 0-100 and also need to check if the price has been set when we are retrieving values

If an item is not set, it will contain the value -1 in the array.

You may need to declare an array of 101 elements for accommodating items 0-100


I think the task is quit clear to you. start working with it. Once you are done then go through the below shown source code and compare. sometimes you may get the same output with a different approach. It doesn't matter because there are more than one way to do the same task in programming.

Source Code:-

#include<iostream>
#include<iomanip>                  //this library is needed to format the output as we want
using namespace std;
int main()
{
const int size=100;
float price_item[size];
int x;
int option;
char decision;

for(int i=1;i<=100;i++)
{
price_item[i]=-1;
}

while(decision!='n')
{
cout<<endl;
cout<<"Menu:"<<endl;
cout<<"1.Store Item Price"<<endl;
cout<<"2.Get Item Price"<<endl<<endl;

cout<<"Enter your option:";
cin>>option;

if(option==1)
{
cout<<endl;
cout<<"Enter Item No:";
cin>>x;

if(x<=0 || x>100)
{
cout<<"Invalid Item No"<<endl;
}

else
{
cout<<"Enter price:";
cin>>price_item[x];
}
}

else if(option==2)
{
cout<<endl;
cout<<"Enter Item No:";
cin>>x;
if(x<=0 || x>100)
{
cout<<"Invalid Item No"<<endl;
}

else if(price_item[x]==-1)
cout<<"Price is not set"<<endl;

else
{
cout<<endl;
cout<<"The Price of Item No"<<x<<" is Rs."<<setiosflags(ios::fixed)<<setprecision(2)<<price_item[x]<<endl;
}
}

else
cout<<"Please enter an available option!"<<endl;
cout<<endl;
cout<<"Do you want to continue(y/n):";
cin>>decision;

if(decision=='y')
continue;

}
cout<<endl;
cout<<"Goodbuy and have a nice day...!"<<endl;
return 0;
}

note that  setiosflags() and setprecision() methods are available in iomanip library.