Please help with array
Aug 12, 2015 at 3:34pm UTC
I have been trying to figure this out but can't seem to figure it out. Please help -- I'm new to Array functions
Modify the program to be able to accept up to 100 items?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string name1, name2, name3;
double price1, price2, price3;
double subtotal, tax, total;
const double TAX_RATE = 0.095;
cout << "\nPlease enter the names and prices of the items you wish "
<< "to purchase:\n" ;
cout << "Item 1: " ;
cin >> name1 >> price1;
cout << "Item 2: " ;
cin >> name2 >> price2;
cout << "Item 3: " ;
cin >> name3 >> price3;
subtotal = price1 + price2 + price3;
tax = subtotal * TAX_RATE;
total = subtotal + tax;
cout << endl;
cout << left << setw(10) << "item"
<< right << setw(10) << "price" << endl
<< "--------------------" << endl
<< setprecision(2) << fixed
<< left << setw(10) << name1
<< right << setw(10) << price1 << endl
<< left << setw(10) << name2
<< right << setw(10) << price2 << endl
<< left << setw(10) << name3
<< right << setw(10) << price3 << endl
<< "--------------------" << endl
<< left << setw(10) << "subtotal"
<< right << setw(10) << subtotal << endl << endl
<< left << setw(10) << "tax"
<< right << setw(10) << tax << endl
<< left << setw(10) << "total"
<< right << setw(10) << total << endl << endl;
return 0;
}
Here's what I tried:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <iostream>
#include <conio.h>
#include<string>
using namespace std;
int main()
{
const int size = 100;
string product[size];
double value[100], subtotal, tax, total;
const double rate = 0.095;
cout << "\n Enter the name of the products 1 to 100 " << endl;
for (int prodcnt = 0; prodcnt<size; prodcnt++)
{
cout << "\t\t" << prodcnt + 1 << "\t\t" ;
cin >> product[prodcnt];
}
cout << "\n Enter the values from 1 to 100 " << endl;
for (int val = 0; val < size; val++)
{
cout << "\t\t" << val + 1 << "\t\t" ;
cin >> value[val];
for (int i = 1; i <= size; i++)
{
subtotal = val + i;
}
}
cout << "Subtotal = " << subtotal;
tax = subtotal * rate;
total = subtotal + tax;
return 0;
Last edited on Aug 12, 2015 at 5:45pm UTC
Aug 12, 2015 at 3:39pm UTC
1) Please use code tags when posting code, to make it readable:
http://www.cplusplus.com/articles/z13hAqkS/
2) What's your problem? Build error? Crash? Something else? We're not mind-readers - we need you to actually give us details of what the problem is.
Aug 12, 2015 at 3:56pm UTC
I enter 1-4 values and their prices, but then when I enter CTRL Z to stop the loop. it just repeats itself up to 100 with 0 values and 0 prices.
I tried using break; to end the loop but that didn't work, and I also tried writing the code a different way but same problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
const int size = 100;
string item[size];
double price[size], subtotal = 0;
cout << "Enter the item and price" << endl;
for (int counter = 0; counter < size; counter++)
{
cout << "Item: " << (counter + 1);
cin >> item[counter];
cout << "price" << (counter + 1);
cin >> price[counter];
}
cout << "Here are the items:" << endl;
cout << fixed << showpoint << setprecision(2);
for (int counter = 0; counter < size; counter++)
{
double subtotal = price[counter];
cout << "subtotal:" << subtotal << endl;
}
system("pause" );
return 0;
}
Topic archived. No new replies allowed.