Hi people...
I'm still new in c++..
I got the problem with the recent assignment
that i have to do...
This is the assignment that i got...

[ATTACH]12228[/ATTACH]

my problem is i dont know how to display all the value that the user entered into a single receipt...when i tried to run my program,its only could display the last value entered by user...After i figured out..i know that i suppose to stored those values in the array..but the problem is i still dont know how to display all the data that stored in the array that I have declared...This is my attempt..

#include <iostream>
#include <iomanip>
using namespace std;

int Id,price,quantity,items,sale;
int no;
string name;


//FUNCTION PROTOTYPE
void ENTER();
void ENTER1 ();
int display();
int add_item();

int main ()
{
  cout<<"------------------------PROGRAM 2 PROJECT [CASH RECEIPT]------------------------"<<endl;
  ENTER();

  cout<<endl;

cin.get();
return 0;

}


void ENTER ()
{
    cout<<"Enter the Product ID :";
    cin>>Id;
    cout<<"Enter the Product Name :";
    cin>>name;
    cout<<"Enter the Price For Single Item :";
    cin>>price;
    cout<<"Enter The Quantity :";
    cin>>quantity;
    cout<<endl;

    add_item();
}

int Id1;
string name1;
int price1,quantity1;
void ENTER1 ()
{
    cout<<"Enter the Product ID :";
    cin>>Id1;
    cout<<"Enter the Product Name :";
    cin>>name1;
    cout<<"Enter the Price For Single Item :";
    cin>>price1;
    cout<<"Enter The Quantity :";
    cin>>quantity1;
    cout<<endl;

    add_item();
}




int display ()
{
    cout<<Id<<setw(10)<<name<<setw(10)<<price<<setw(10)<<quantity<<endl;
    cout<<Id1<<setw(10)<<name1<<setw(10)<<price1<<setw(10)<<quantity1;
}


int add_item()
{
    char add;
    cout<<"Add Another Item (y/n) :";
    cin>>add;

    cout<<endl;

    switch (add){

        case 'y' :
           ENTER1 ();
           break;

        case 'n' :
            display();
            break;
    }
}

Recommended Answers

All 3 Replies

When it comes to storing a bunch of records, nothing beats what I like to call an 'array of structs'.

First, design a container that you would like to use:

stuct record{

     string id;
     string  name;
     double price;
     int quantity;
};

Now that you have a good design for your container, you just need to create a bunch of them:

record product_info[100];

You now have 100 struct objects that you can populate.

int i=0;
char answer;

do{
     
     cout << "\nEnter the Product ID :  ";
     cin >> product_info[i].id;

     cout << "\nEnter the Product Name :  ";
     cin >> product_info[i].name;

     cout << "\nEnter the Price For Single Item :  ";
     cin >> product_info[i].price;

     cout << "\nEnter The Quantity : ";
     cin >> product_info[i].quantity;

     cout << "\nWould ye' like to enter another product? (Y/N) ";
     cin >> answer;

     i++;

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

At this point you now have a bunch of containers full of product information. You can perform any operations on the product_info[ ] array as you wish. Here is an example operation; displaying info for a specific product (in this example, based on its product ID):

string input;
int index=0;
bool found = FALSE;

cout << "\nSearch by Product ID:  ";
cin >> input;

// search through the array and find the record based on its ID
do{    
     if(input == product_info[index].id)
     {
          found = TRUE;
     }
     
     else
     {
          index++;
     }
}while(! found  && index < number_of_entries);

// error handling
if(! found)
{
     cout << "\n\aProduct ID: " << input << " does not exist in our 
              records. ";
}

// 'index' should now be the location of the record in the product_info[] array.
else
{
     // Display all the info about that product
     cout  << "\nInformation for Product ID:  "  
           << product_info[index].id;
     cout << "\nName:  " <<  product_info[index].name;
     cout << "\nPrice:  " <<  product_info[index].price;
     cout << "\nQuantity:  " <<  product_info[index].quanity; 
}

Throughout your CS academic career, you will often be tasked with creating a bunch of records of stuff; whether it be airline ticket information, bank accounts, products, receipts, phone book... whatever the case may be. In these situations, the container I immediately think of for storing all these records is an array of structs.

owh...Thank you Clinton...
Now I already can understand how it should be programmed.. :)
But here is the other problem that i got..

I dont know whats wrong with my code..
I want the program to display the sale for each item
which is price times with the quantity..i declared sale to be as double as i want it to be in point..
But when i run this program the sale number is not correct.Instead of showing price time with the quantity it shows me the weird numbers..

So if you dont mind..could you tell me a little bit more of why my codes were wrong and how should i correct it??

Below is the code that i've written base on your guidance before..

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main () {

class record{
 public :
   string id;
   string name;
   double price;
   double quantity;
   };

   record product_info[100];



      int i=0;
      char answer;

      cout<<"--------------------------------------------------------------------------------";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"--------------------------------------------------------------------------------";
      cout<<endl;

      do{

      cout << "Enter the Product ID : ";
      cin >> product_info[i].id;


      cout << "Enter the Product Name : ";
      cin >> product_info[i].name;


      cout << "Enter the Price For Single Item : ";
      cin >> product_info[i].price;

      cout << "Enter The Quantity : ";
      cin >> product_info[i].quantity;

      cout<<endl;

      cout << "Would You like to enter another product? (Y/N) ";
      cin >> answer;

      i++;

      cout<<endl;

      }while(answer == 'y' || answer == 'Y' && answer !='N'&&'n');


if (answer == 'N' || 'n'){
int index;
double price=product_info[i].price;
double quantity=product_info[i].quantity;


double sale=(price)*(quantity);


 for(index=0; index<i; index++){
// Display all the info about that product
cout<<"--------------------------------------------------------------------------------";
cout<<"ID |"<<setw(13)<<"ITEM |"<<setw(11)<<"PRICE |"<<setw(10)<<"QTY |"<<setw(10)<<"SALE"<<endl;
cout<<"--------------------------------------------------------------------------------";
cout <<product_info[index].id<<setw(10)<<product_info[index].name<<setw(10)<<"$"<<product_info[index].price
<<setw(10)<<product_info[index].quantity<<setw(10)<<sale;
cout<<endl;

}

}

cin.get();

}

i'm really new in this so i really need to learn more,so thank you so much for helping :)

owh...Thank you Clinton...
Now I already can understand how it should be programmed.. :)
But here is the other problem that i got..

I dont know whats wrong with my code..
I want the program to display the sale for each item
which is price times with the quantity..i declared sale to be as double as i want it to be in point..
But when i run this program the sale number is not correct.Instead of showing price time with the quantity it shows me the weird numbers..

So if you dont mind..could you tell me a little bit more of why my codes were wrong and how should i correct it??

Below is the code that i've written base on your guidance before..

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main () {

class record{
 public :
   string id;
   string name;
   double price;
   double quantity;
   };

   record product_info[100];



      int i=0;
      char answer;

      cout<<"--------------------------------------------------------------------------------";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"--------------------------------------------------------------------------------";
      cout<<endl;

      do{

      cout << "Enter the Product ID : ";
      cin >> product_info[i].id;


      cout << "Enter the Product Name : ";
      cin >> product_info[i].name;


      cout << "Enter the Price For Single Item : ";
      cin >> product_info[i].price;

      cout << "Enter The Quantity : ";
      cin >> product_info[i].quantity;

      cout<<endl;

      cout << "Would You like to enter another product? (Y/N) ";
      cin >> answer;

      i++;

      cout<<endl;

      }while(answer == 'y' || answer == 'Y' && answer !='N'&&'n');


if (answer == 'N' || 'n'){
int index;
double price=product_info[i].price;
double quantity=product_info[i].quantity;


double sale=(price)*(quantity);


 for(index=0; index<i; index++){
// Display all the info about that product
cout<<"--------------------------------------------------------------------------------";
cout<<"ID |"<<setw(13)<<"ITEM |"<<setw(11)<<"PRICE |"<<setw(10)<<"QTY |"<<setw(10)<<"SALE"<<endl;
cout<<"--------------------------------------------------------------------------------";
cout <<product_info[index].id<<setw(10)<<product_info[index].name<<setw(10)<<"$"<<product_info[index].price
<<setw(10)<<product_info[index].quantity<<setw(10)<<sale;
cout<<endl;

}

}

cin.get();

}

i'm really new in this so i really need to learn more,so thank you so much for helping :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.