Vector Program that Displays User Input into a table

gpjacks 0 Tallied Votes 252 Views Share

This is a program which declares two vectors of 6 elements each of type double. One vector is named weight and the other is named rate. The program uses a loop to prompt the user for a weight in ounces and a postage rate for that weight and then stores the values in a parallel element of vectors. After the six weights and rates have been gathered and stored in the vectors, a for loop will print a table of weights and rates to the screen.

Now the program prompts the user for how many packages they want to enter. Then it prompts the user how much the package weighs and what the postage rate is. After the user has entered all the data a table is printed out of the weights and rates that they entered.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>

using namespace std;


int main()
{
	
	int items;
	int index;   // counter for loops
	
	ifstream infile_weight; // pointer file for weight
	ifstream infile_rate; // pointer file for rate

	vector <double> weight (6); // vector with 6 elements
	vector <double> rate (6); 

	infile_weight.open("weight.dat",ios::in); // open file for input
	infile_rate.open("rate.dat",ios::in);
	
         cout << "How many packages do you want to enter? ";
	 cin >> items;
	 // resize to hold # of items user wants to enter
	 weight.resize(items);
	 rate.resize(items);
	 // loop to prompt user and index items
	 for (index = 0; index <=(items - 1); index++)
	 {
		cout << "Package # " << index + 1 << ":\n";
		cout << "How much does your package weigh? ";
		cin >> weight[index];
		cout << "What is the postage rate? ";
		cin >> rate[index];
	 }
	 
	 cout.setf(ios::showpoint);
         cout.setf(ios::fixed);
	 cout << setprecision(2);

	 cout << "Weight" << "     " << setw(10) << "Rate" <<  endl;
	 // print table
	 for (index = 0; index <=(items - 1); index++)
	 {
		 cout << weight[index] << setw (17) << rate[index] << endl;
	 }
	 infile_weight.close(); // closing files
	 infile_rate.close();
	 return 0;
}