Project 1
Array in C++ :
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store.
Pointer in C++:
A pointer is a variable that stores the memory address of an object. Pointers are used extensively in both C and C++ for three main purposes:
- to allocate new objects on the heap,
- to pass functions to other functions
- to iterate over elements in arrays or other data structures.
Code:
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
int main()
{
string* Result = new string[5 * 3]; // dynamically allocation of memory
int Laptop[3],Mouse[3],Headset[3],Laptop_bag[3],Adapter[3],Vendor1[5],Vendor2[5]; // creating different array
int s,Vendor1_Total,Vendor2_Total; // local variables
fstream user_input;
user_input.open("User_inputs.txt",ios::out); //opening file in write mode
if(!user_input)
{
cout<<"File not created\n"; // if not created
}
else
{
cout<<"file creation done\n"; // after successfully created
}
while(1) // starting a while loop for displaying menu again and again
{
// Creating menu for user
cout<<"Press 1 to type commodities and purchase Items"<<endl;
cout<<"Press 2 to Enter Vendor quotations"<<endl;
cout<<"Press 3 to find best vendor (Only select when you have enter commodities and vendors data"<<endl;
cout<<"Press 0 to exit"<<endl;
cin>>s;
user_input << "Selected option" << endl;
user_input << s << endl; // taking 's' as input to take next step in loop
if (s == 0) // is 's' equal to zero then break loop. and terminate program
{
break;
}
else if (s == 1) // is 's' equal to 1
{
cout<<"Please enter the name of commodities"<<endl;
user_input << "Name of commodities" << endl;
for (int i = 0; i < 5; i++) // here this for for loop is for taking item name from user
{
for (int j = 0; j < 1; j++)
{
cin>> *(Result + i*3 + j); // assign values to the allocated memory for commodities only
user_input << *(Result + i*3 + j) << endl;
}
}
cout<<"Enter number of Laptop Ramu,Raju and Shyam wants to purchase"<<endl; // taking value for laptops
user_input << "Laptop" << endl;
for(int i=0;i<3;i++)
{
cin>>Laptop[i];
user_input <<Laptop[i] << endl;
}
cout<<"Enter number of Mouse Ramu,Raju and Shyam want to purchase"<<endl; // taking value for mouses
user_input << "Mouse" << endl;
for(int i=0;i<3;i++)
{
cin>>Mouse[i];
user_input <<Mouse[i] << endl;
}
cout<<"Enter number of Headset Ramu,Raju and Shyam want to purchase"<<endl; // taking value for headset
user_input << "Headest" << endl;
for(int i=0;i<3;i++)
{
cin>>Headset[i];
user_input <<Headset[i] << endl;
}
cout<<"Enter number of Laptop Bag Ramu,Raju and Shyam want to purchase"<<endl; // taking value for labtop bag
user_input << "Laptop Bag" << endl;
for(int i=0;i<3;i++)
{
cin>>Laptop_bag[i];
user_input <<Laptop_bag[i] << endl;
}
cout<<"Enter number of Adapter Ramu,Raju and Shyam want to purchase"<<endl; // taking value for adapter
user_input << "Adapter" << endl;
for(int i=0;i<3;i++)
{
cin>>Adapter[i];
user_input << i << endl;
user_input <<Adapter[i] << endl;
}
}
else if (s == 2) // is 's' equal to 2 then taking values for both Vendors Quotations
{
cout<<"Enter Vendor-1 Quotations"<<endl;
user_input << "Vendor-1 Quotations" << endl;
for (int i=0;i<5;i++) // for vender-1
{
cin>>Vendor1[i];
user_input <<Vendor1[i] << endl;
}
Vendor1_Total = Vendor1[0] + Vendor1[1] + Vendor1[2] + Vendor1[3] + Vendor1[4];
cout<<"Enter Vendor-2 Quotations"<<endl;
user_input << "Vendor2 Quotations" << endl;
for (int i=0;i<5;i++) // for vendor-2
{
cin>>Vendor2[i];
user_input <<Vendor2[i] << endl;
}
Vendor2_Total = Vendor2[0] + Vendor2[1] + Vendor2[2] + Vendor2[3] + Vendor2[4];
}
else if ( s == 3) // after selecting 1 and 2 user then select 's' equal to 3 then calculation take place
{
if (Vendor1_Total < Vendor2_Total) // printing results
{
cout<<"The best vendor for Ramu is Vender-1"<<endl;
cout<<"The best vendor for Ramu is Vender-1"<<endl;
cout<<"The best vendor for Ramu is Vender-1"<<endl;
}
else
{
cout<<"The best vendor for Ramu is Vender-2"<<endl;
cout<<"The best vendor for Ramu is Vender-2"<<endl;
cout<<"The best vendor for Ramu is Vender-2"<<endl;
}
}
else // for invalid data
cout<<"Invalid input please try again"<<endl;
}
return 0;
}
.txt file output:
(1)

(2)

Console Output:
(1)

(2)
