Challenge

Project 1

Solution:

//Preprocessor Section/Header files

#include <iostream> //Standard I/O Stream Library

#include <cstring> //Standard C String Library

#include <fstream>

using namespace std;

//Main Function

int main()

{

//Declare an array of strings to store commodity names

string* Result = new string[5 * 3]; //Dynamically allocate memory for commodity names

//Declare arrays for quantities of different items, and local variables.

int Laptop[3], Mouse[3], Headset[3], Laptop_bag[3], Adapter[3], Vendor1[5], Vendor2[5];

int s, Vendor1_Total, Vendor2_Total;

//Create a file stream for user input

fstream input_data;

input_data.open("input_data.txt", ios::out);//Open the file in write mode

if (input_data)

{

cout<<"Select the option From Menu"<<endl; //If the file is not created

}

while (1) //Start an infinite while loop for displaying the menu repeatedly

{

//Display a menu for input data options

cout<<"1. Press 1 to type commodities and purchase items"<<endl;

cout<<"2. Press 2 to Enter Vendor quotations"<<endl;

cout<<"3. Press 3 to find the best vendor(Only select when you have entered commodities and vendors data)"<<endl;

cout<<"4. Press 0 to exit"<<endl;

cin>>s;

input_data<<"Selected option"<<endl;

input_data<<s<<endl; //Record the selected option in the file

if (s == 0) //If s is equal to zero break the loop and terminate the program

{

break;

}

else if (s == 1) //If s is equal to 1, gather commodity and quantity data

{

cout<<"Please enter the names of commodities"<<endl;

input_data<<"Name of commodities"<<endl;

for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 1; j++)

{

cin>>*(Result + i * 3 + j); //Assign values to the allocated memory for commodities

input_data<<*(Result + i * 3 + j)<<endl; //Record commodity names in the file

}

}

// Prompt the user to enter the number of items they want to purchase for different categories

cout <<"Enter the number of Laptops Ramu, Raju, and Shyam want to purchase"<<endl;

input_data<<"Laptop"<<endl;

for (int i = 0; i < 3; i++)

{

cin>>Laptop[i];

input_data << Laptop[i] << endl;

}

cout<<"Enter the number of Mouse Ramu, Raju, and Shyam want to purchase"<<endl;

input_data<<"Mouse"<<endl;

for (int i = 0; i < 3; i++)

{

cin>>Mouse[i];

input_data<<Mouse[i]<<endl;

}

cout<<"Enter the number of Headsets Ramu, Raju, and Shyam want to purchase"<<endl;

input_data<<"Headset"<<endl;

for (int i = 0; i < 3; i++)

{

cin>>Headset[i];

input_data<<Headset[i]<<endl;

}

cout<<"Enter the number of Laptop Bags Ramu, Raju, and Shyam want to purchase"<<endl;

input_data<<"Laptop Bags"<<endl;

for (int i = 0; i < 3; i++)

{

cin>>Laptop_bag[i];

input_data<<Laptop_bag[i]<<endl;

}

cout<<"Enter the number of Adapters Ramu, Raju, and Shyam want to purchase"<<endl;

input_data<<"Adapters"<<endl;

for (int i = 0; i < 3; i++)

{

cin>>Adapter[i];

input_data<<i<<endl;

input_data<<Adapter[i]<<endl;

}

}

else if (s == 2) //If s is equal to 2, collect vendor quotations

{

cout<<"Enter Vendor-1 Quotations"<<endl;

input_data<<"Vendor-1 Quotations"<<endl;

for (int i = 0; i < 5; i++) //Loop to collect Vendor-1 quotations

{

cin>>Vendor1[i];

input_data<<Vendor1[i]<<endl;

}

Vendor1_Total = Vendor1[0] + Vendor1[1] + Vendor1[2] + Vendor1[3] + Vendor1[4];

cout<<"Enter Vendor-2 Quotations"<<endl;

input_data<<"Vendor-2 Quotations"<<endl;

for (int i = 0; i < 5; i++) //Loop to collect Vendor-2 quotations

{

cin>>Vendor2[i];

input_data<<Vendor2[i]<<endl;

}

Vendor2_Total = Vendor2[0] + Vendor2[1] + Vendor2[2] + Vendor2[3] + Vendor2[4];

}

else if (s == 3) //If s is equal to 3, perform calculations and display the best vendor

{

if (Vendor1_Total < Vendor2_Total)

{

cout<<"The best vendor for Ramu is Vendor-1"<<endl;

cout<<"The best vendor for Raju is Vendor-1"<<endl;

cout<<"The best vendor for Shyam is Vendor-1"<<endl;

}

else

{

cout<<"The best vendor for Ramu is Vendor-2"<<endl;

cout<<"The best vendor for Raju is Vendor-2"<<endl;

cout<<"The best vendor for Shyam is Vendor-2"<<endl;

}

}

else

{

cout<<"Invalid input, please try again"<<endl;

}

}

delete[] Result;

return 0;

}

Output:

Select the option From Menu

1. Press 1 to type commodities and purchase items

2. Press 2 to Enter Vendor quotations

3. Press 3 to find the best vendor(Only select when you have entered commodities and vendors data)

4. Press 0 to exit

1

Please enter the names of commodities

Laptop

Mouse

Headset

Laptop Bag

Enter the number of Laptops Ramu, Raju, and Shyam want to purchase

5

10

3

Enter the number of Mouse Ramu, Raju, and Shyam want to purchase

15

10

5

Enter the number of Headsets Ramu, Raju, and Shyam want to purchase

10

25

6

Enter the number of Laptop Bags Ramu, Raju, and Shyam want to purchase

5

10

3

Enter the number of Adapters Ramu, Raju, and Shyam want to purchase

20

15

10

1. Press 1 to type commodities and purchase items

2. Press 2 to Enter Vendor quotations

3. Press 3 to find the best vendor(Only select when you have entered commodities and vendors data)

4. Press 0 to exit

2

Enter Vendor-1 Quotations

45000

1500

2500

1200

1600

Enter Vendor-2 Quotations

43000

1200

3000

1000

1500

1. Press 1 to type commodities and purchase items

2. Press 2 to Enter Vendor quotations

3. Press 3 to find the best vendor(Only select when you have entered commodities and vendors data)

4. Press 0 to exit

3

The best vendor for Ramu is Vendor-2

The best vendor for Raju is Vendor-2

The best vendor for Shyam is Vendor-2

1. Press 1 to type commodities and purchase items

2. Press 2 to Enter Vendor quotations

3. Press 3 to find the best vendor(Only select when you have entered commodities and vendors data)

4. Press 0 to exit

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.