0

hi i ave an assignment on point of sale terminal where i have to write a program so that it displays the product ID, amount of product , cost of each product and then the total cost.

the following code shows how i've started it. Could some one tell me if i'm doing it right and what's rong with the code because it's not executing.

i'll really appreciate your help. Thank you.

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

struct itemPrice  // this is a global declaration
{
       char id;
       double price;
};


int main()

{
    const int ITEMS= 2000;  // maximum number of items
    int i;
    itemPrice list[ITEMS] ={ 
                         { P010001		24.99}, 	
                         {QX-35		19.99},
                         {DVD-player5	58.95},
                         {P010002		 8.85},	
                         {P010003		35.00}
                         };
   cout<< endl; //start on a new line 
   cout<< setiosflags (ios: :left); //left justify the output
   for(i=o; i < ITEMS; i++)
   
   cout << set(7)           << list.id
        << setw(15)         << list.price
        <<endl;
         

system ("pause");
return 0;
}
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Nick Evan
0

A lot of things are wrong with this code. But before I begin, you might want to read this about code tags.

I'll start at the top:

struct itemPrice // this is a global declaration
{
char id;
double price;
};

Id is a char, but later in your code, you try to put things like 'P010001 ' in it, which has more then one char.
So I recommend you use a string for id:

struct itemPrice // this is a global declaration
{
string id;
double price;
};

Don't forget to #include <string> Next: const int ITEMS= 2000; // maximum number of items Why 2000? You only put 5 items in it. Later in your code your going to loop trough these elements, so if 1995 haven't got any values you'll get a memory-exception. So change it to : const int ITEMS= 5; // maximum number of items Then:

itemPrice list[ITEMS] ={
{ P010001 24.99}, 
{etc etc etc}

That's just wrong. Place the id between "quotes" and seperate the two values with a comma:

itemPrice list[ITEMS] ={
{ "P010001", 24.99}, 
//etc etc

for(i=o; i < ITEMS; i++) That's a typo. You have a 'o' instead of a 0 (zero), so: for(i=0; i < ITEMS; i++) Last but not least: system ("pause"); . This is known to be bad coding. This will only work on a dos/windows system. To keep the console open until you press a key use: cin.get() instead of system("pause");

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.