#include<iostream.h> //is used so it will display output to the user and that input can be read from the keyboard
#include<iomanip.h> //is used so that the user can format input
using namespace std;

int main()

{
  
    int qty1;
    cout << "Please enter the quantity of Televisions: "<<endl;
    cin >> qty1;
    int qty2;
    cout << "Please enter the quantity of Video Recorder: "<<endl;
    cin >> qty2;
    int qty3;
    cout << "Please enter the quantity of Camera: "<<endl;
    cin >> qty3;
    int qty4;
    cout << "Please enter the quantity of DVD: "<<endl;
    cin >> qty4;
    int qty5;
    cout << "Please enter the quantity of HiFi Stereo: "<<endl; 
    cin >> qty5;
    int qty6;
    cout << "Please enter the quantity of Video CD: " <<endl;
    cin >> qty6; 
    
    int price1;
    cout << "Please enter the price of Televisions: "<<endl;
    cin >> price1;
    int price2;
    cout << "Please enter the price of Video Recorder: "<<endl;
    cin >> price2;
    int price3;
    cout << "Please enter the price of Camera: "<<endl;
    cin >> price3;
    int price4;
    cout << "Please enter the price of DVD: "<<endl;
    cin >> price4;
    int price5;
    cout << "Please enter the price of HiFi Stereo: "<<endl; 
    cin >> price5;
    int price6;
    cout << "Please enter the price of Video CD: " <<endl;
    cin >> price6;
     
     float totalStockValue1 = qty1 * price1; //the equation that calculates the price of the item multipled by the quantity
     float totalStockValue2 = qty2 * price2;
     float totalStockValue3 = qty3 * price3;
     float totalStockValue4 = qty4 * price4;
     float totalStockValue5 = qty5 * price5;
     float totalStockValue6 = qty6 * price6;

     float totalGrossValue = totalStockValue1 + totalStockValue2 + totalStockValue3 + totalStockValue4 + totalStockValue5 + totalStockValue6; //calculates the total value of all the inputs
     
     
     cout<<setiosflags(ios::fixed)<<setprecision(2); //sets the output to 2 decimal places
     cout<<setiosflags(ios::fixed)<<setprecision(2);
     cout<<setiosflags(ios::fixed)<<setprecision(2);
     
     cout<<setw(15)<< "Item" //fomats the output
         <<setw(11)<< "Quantity"
         <<setw(13)<< "Unit Price"
         <<setw(14)<< "Total Value"<<endl;
     
cout<<"============================================================="<<endl;
     
     cout<<setw(15)<< "Television"
         <<setw(11)<< qty1
         <<setw(13)<< price1
         <<setw(14)<< totalStockValue1<<endl;
     cout<<setw(15)<< "Video Recorder"
         <<setw(11)<< qty2
         <<setw(13)<< price2
         <<setw(14)<< totalStockValue2<<endl;   
     cout<<setw(15)<< "Camera"
         <<setw(11)<< qty3
         <<setw(13)<< price3
         <<setw(14)<< totalStockValue3<<endl;
     cout<<setw(15)<< "DVD"
         <<setw(11)<< qty4
         <<setw(13)<< price4
         <<setw(14)<< totalStockValue4<<endl; 
     cout<<setw(15)<< "HiFi Stereo"
         <<setw(11)<< qty5
         <<setw(13)<< price5
         <<setw(14)<< totalStockValue5<<endl; 
     cout<<setw(15)<< "Video CD"
         <<setw(11)<< qty6
         <<setw(13)<< price6
         <<setw(14)<< totalStockValue6<<endl;

cout<<"============================================================="<<endl;

cout<<setw(45)<< "Gross Total: "
    <<setw(4)<<gross<<totalGrossValue<<endl;

 system("pause"); 

}

i have this code here which when the client enters the quantity and the price for each item. i know the code is excessive but this is the only way i know i could make it work. can someone tell me how i change it to arrays and for or while loops?

Recommended Answers

All 9 Replies

Create an enum for the different types of components. Then create an array indexed from 0 to NUM_COMPONENTS - 1 for the price, quantity, and string name of each component. You'll access the components in the array by their enumerated value, like quantities[CAMERA]. Finally, loop over the enumerated values to grab input and produce output.

ok i understand that but where do i put it in the code.
and how do i write it before each question.

// Set up the enum... I'll do it for just two components
enum ComponentTypes = { CT_CAMERA, CT_DVD, CT_LAST };

// Set up the arrays
string names[CT_LAST] = { "Camera", "DVD" };
int quantities[CT_LAST];
int prices[CT_LAST];

// Read in the quantities
for (int i = 0; i < CT_LAST; ++i)
{
   cout << "Input the price of the " << names[i] << ": ";
   cin >> prices[i];
}

// Read in prices in the same way

// Do your arithmetic

// Output result

ok thank you

im getting this error use of enum `ComponentTypes' without previous declaration
where do i declare it.

Anywhere before you use it. The top of the file should work.

ok this is my code now

#include<iostream.h> //is used so it will display output to the user and that input can be read from the keyboard
#include<iomanip.h> //is used so that the user can format input
using namespace std;

void getQuanity(int&,int);
void getStock(int, int&);
void gross(int[],int,int);
int main()

{
int ComponentTypes = 0;
int names = 0;
int prices = 0;
int quantities =0;


enum ComponentTypes = { CT_TELEVISION, CT_VIDEORECORDER, CT_CAMERA, CT_DVD, CT_HIFISTEREO, CT_VIDEOCD, CT_LAST };
string names[CT_LAST] = { "Televisions", "Video Recorder","Camera","DVD","HIFI Stereo","Video CD" };
int quantities[CT_LAST];
int prices[CT_LAST];

for (int a = 0; a < CT_LAST; ++a)

{

cout << "Input the price of the " << names[a] << ": ";

cin >> prices[a];
}

for (int b = 0; b < CT_LAST; ++b)

{

cout << "Input the quantity of the " << names[b] << ": ";

cin >> prices[b];

}
     
     float totalStockValue1 = qty1 * price1; //the equation that calculates the price of the item multipled by the quantity
     float totalStockValue2 = qty2 * price2;
     float totalStockValue3 = qty3 * price3;
     float totalStockValue4 = qty4 * price4;
     float totalStockValue5 = qty5 * price5;
     float totalStockValue6 = qty6 * price6;

     float totalGrossValue = totalStockValue1 + totalStockValue2 + totalStockValue3 + totalStockValue4 + totalStockValue5 + totalStockValue6; //calculates the total value of all the inputs
     
     
     cout<<setiosflags(ios::fixed)<<setprecision(2); //sets the output to 2 decimal places
     cout<<setiosflags(ios::fixed)<<setprecision(2);
     cout<<setiosflags(ios::fixed)<<setprecision(2);
     
     cout<<setw(15)<< "Item" //fomats the output
         <<setw(11)<< "Quantity"
         <<setw(13)<< "Unit Price"
         <<setw(14)<< "Total Value"<<endl;
     
cout<<"============================================================="<<endl;
     
     cout<<setw(15)<< "Television"
         <<setw(11)<< qty1
         <<setw(13)<< price1
         <<setw(14)<< totalStockValue1<<endl;
     cout<<setw(15)<< "Video Recorder"
         <<setw(11)<< qty2
         <<setw(13)<< price2
         <<setw(14)<< totalStockValue2<<endl;   
     cout<<setw(15)<< "Camera"
         <<setw(11)<< qty3
         <<setw(13)<< price3
         <<setw(14)<< totalStockValue3<<endl;
     cout<<setw(15)<< "DVD"
         <<setw(11)<< qty4
         <<setw(13)<< price4
         <<setw(14)<< totalStockValue4<<endl; 
     cout<<setw(15)<< "HiFi Stereo"
         <<setw(11)<< qty5
         <<setw(13)<< price5
         <<setw(14)<< totalStockValue5<<endl; 
     cout<<setw(15)<< "Video CD"
         <<setw(11)<< qty6
         <<setw(13)<< price6
         <<setw(14)<< totalStockValue6<<endl;

cout<<"============================================================="<<endl;

cout<<setw(45)<< "Gross Total: "
    <<setw(4)<<gross<<totalGrossValue<<endl;

 system("pause"); 

}

do i just make

int ComponentTypes = 0;
int names = 0;
int prices = 0;
int quantities =0;

also for

float totalStockValue1 = qty1 * price1; //the equation that calculates the price of the item multipled by the quantity
     float totalStockValue2 = qty2 * price2;
     float totalStockValue3 = qty3 * price3;
     float totalStockValue4 = qty4 * price4;
     float totalStockValue5 = qty5 * price5;
     float totalStockValue6 = qty6 * price6;

     float totalGrossValue = totalStockValue1 + totalStockValue2 + totalStockValue3 + totalStockValue4 + totalStockValue5 + totalStockValue6; //calculates the total value of all the inputs

what are the names now for all of them is it

Why are you doing this:

int ComponentTypes = 0;
int names = 0;
int prices = 0;
int quantities = 0;

You're declaring those as integers, and then trying to redeclare them. Why? Your source file should just look like this:

// #includes here

// Declare the enumeration
enum ComponentTypes = { /* Your enumerated types here */, CT_LAST };

// Function declarations here

// Declare the arrays
string names[CT_LAST] = { /* Your names here */ };
int prices[CT_LAST];
int quantities[CT_LAST];

// Main goes down here
int main()
{
   /* Do lots of cool stuff */
}

As for your last question, you don't even need to store those values as variables. Just use prices[index] * quantities[index] instead.

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

enum ComponentTypes = { CT_TELEVISION, CT_VIDEORECORDER, CT_CAMERA, CT_DVD, CT_HIFISTEREO, CT_VIDEOCD, CT_LAST };
int CT_LAST = 0;
string names[CT_LAST] = { "Televisions", "Video Recorder","Camera","DVD","HIFI Stereo","Video CD" };
int quantities[CT_LAST];
int prices[CT_LAST];
int totalStockValue [CT_LAST];
int main()

{
for (int a = 0; a < CT_LAST; ++a)

{

cout << "Input the price of the " << names[a] << ": ";

cin >> prices[a];
}

for (int b = 0; b < CT_LAST; ++b)

{

cout << "Input the quantity of the " << names[b] << ": ";

cin >> quantities[b];

}
 
     
     cout<<setiosflags(ios::fixed)<<setprecision(2); 
     cout<<setiosflags(ios::fixed)<<setprecision(2);
     cout<<setiosflags(ios::fixed)<<setprecision(2);
     
     cout<<setw(15)<< "Item" //fomats the output
         <<setw(11)<< "Quantity"
         <<setw(13)<< "Unit Price"
         <<setw(14)<< "Total Value"<<endl;
     
cout<<"============================================================="<<endl;
     
     cout<<setw(15)<< "Television"
         <<setw(11)<< quantities[CT_TELEVISION]/*when i tried this it didn't work because it said undeclared, what do i write in here*/
         <<setw(13)<< prices[CT_TELEVISION]
         <<setw(14)<< endl;
     cout<<setw(15)<< "Video Recorder"
         <<setw(11)<< quantities[CT_VIDEORECORDER]
         <<setw(13)<< prices[CT_VIDEORECORDER]
         <<setw(14)<< endl;   
     cout<<setw(15)<< "Camera"
         <<setw(11)<< quantities[CT_CAMERA]
         <<setw(13)<< prices[CT_CAMERA]
         <<setw(14)<< endl;
     cout<<setw(15)<< "DVD"
         <<setw(11)<< quantities[CT_DVD]
         <<setw(13)<< prices[CT_DVD]
         <<setw(14)<< endl; 
     cout<<setw(15)<< "HiFi Stereo"
         <<setw(11)<< quantities[CT_HIFISTEREO]
         <<setw(13)<< prices[CT_HIFISTEREO]
         <<setw(14)<< endl; 
     cout<<setw(15)<< "Video CD"
         <<setw(11)<< quantities[CT_VIDEOCD]
         <<setw(13)<< prices[CT_VIDEOCD]
         <<setw(14)<< endl;

cout<<"============================================================="<<endl;



 system("pause"); 

}
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.