Hi..
I'm writing a code for the cash receipt program....
before this,i already make this kind of program..

but the program that i've made before is simply doing this task only :



//ask the user to enter the product information...
Enter the product id :
Enter the product name :
Enter the price for a single item :
Enter the quantity :


would you like to enter another item ? :

//then this program will display the product id,item,price,quantity,and sale....

as you can see from above,the program that i've made before will take the informations of the product one by one,and then display them back together with the price and also the product sale...

But,I wanna try something that is a little bit different...
I want to make a program that will ask the user to enter only the product id,then the program will automaticly identify its name and its price and finally display them together with the product sale....

here is attempt :

#include <iostream>
#include<iomanip>

using namespace std;

int main () {

    struct record {

        int id;
        string item;
    };


    record product[100];
    int i=0,t=0;
    char answer;

    do {

    cout<<"Enter The Product Id :";
    cin>>product[i].id;

    cout<<"Enter more item ?";
    cin>>answer;


    }while (answer=='y' || answer=='Y');


switch (product[i].id){

    case 1001 :
    product[t].item="shirt";
    break;

    case 1002 :
    product[t].item="book";
    break;
}

for (int index=0; index<=i; index++)
{
cout<<product[index].item;

}

}

This program is not finish yet because i'm still struggling to display all the names of the products....
This program so far only be able to display the last name of the product only...

I know my mistake here is that is product[t].item="........".
each time the user enter the different product id,the value of
product[t].item will be changed...So thats why at the end,this program will only display the last value which has been assigned to the product[t].item...

I've tried so hard,but so far i still cant figure out what is the actual way to do this....
Please help me....

Thank You :)

Use a database.

You can use SQLite. SQLite makes your life easy by not requiring a server. It creates a simple file and yet provides advanced SQL features.

www.sqlite.org

Install the library and then statically compile your app, so that SQLite goes bundled in with your app, so your user has no worries of installing SQLite in his system.

Comments
What an awful suggestion for the stated problem.

actually this is one of my assignment project...
and in my learning,there will be no database topic will be tought..
so, i need to write this program without using the database...

is there any other way??

>Use a database.
Use your brain. This guy is clearly a beginner, and you want him to struggle with a database API on top of basic C++ for a trivial problem?

>I've tried so hard,but so far i still cant figure out what is the actual way to do this....
You already store the IDs for each product in an array. The problem now is matching the name of the product with the ID after the IDs have all been entered. You started something along those lines with your switch statement, but it should be in a loop and check the current ID at all times. Something like this:

for ( int index = 0; index < i; index++ ) {
  switch ( product[index].id ) {
  case 1001 :
    product[index].item = "shirt";
    break;
  case 1002 :
    product[index].item = "book";
    break;
  default:
    product[index].item = "(unknown)";
    break;
  }
}

Also note the default case. You should always use one, especially when the value being tested is based on user input.

Thank you sooo much...
itech7,all this while i also ever think to make the database and your information maybe will be useful in the future..TQ :)

and Narue ...
You are really help me to solve this....
Thank you so much.... :)

This question has already been answered. Start a new discussion instead.