Arrays of char

Hi,
This Is A Question About Arrays. Plz I need Help.This Is A Simple Code I Wrote.

#include<iostream>
#include<cstdlib>
#include<iomanip>

using namespace std;

main()
{
char name[10];
double tax=0,totax=0,totprice=0,price=0;
cout<<"\nEnter The Tax Rate: ";
cin>>tax;
for(int i=1;i<6;i++)
{
cout<<"\n\n("<<i<<")"<<"Item Name: ";
cin>>name;
cout<<setw(15)<<"Item Price: ";
cin>>price;
totprice=totprice+price;

totax=totax+(price*tax);
cin.ignore();
cout<<setw(10)<<"Item Tax:"<<price*tax;


}
cout<<setw(10)<<"\nTotal Price:"<<totprice;
cout<<setw(10)<<"\nTotal Tax:"<<totax;
cout<<setw(10)<<"\nTotal Price With Tax:"<<totprice+totax;
cin.get();

}

Can Some One Plz Tell Me How This Array Name work?

I Initialize The Array By Allocation The Size To 10. After That Inside The For Loop I'm Taking 5 Item Names(One At A Time). But When Displaying The Names I Entered It Display Only The Current One. Previously Entered Item Name Will Not Display. How This Work Actually. Can Some One Plz Explain This ?????? :(

I think what you had in mind was something like this
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

struct Item
{
     std::string name;
     float price;
};

// my arrays of items

Item itemArray[10]; // allocate 10 items.

int main()
{
      for(int i = 0; i < 6; i++)
      {
             // data entry.
             std::cin >> itemArray[ i ].name;
             std::cin >> itemArray[ i ].price;
      }

      for(int i = 0; i < 6; i++)
      {
            //reproduce list
             std::cout << "Name: " << itemArray[ i ].name;
             std::cout << " Price: " << itemArray[ i ].price << std::endl;
      } 

     // tally total.
     float total = 0;

     for(int i = 0; i < 6; i++)
     {
           total+=itemArray[ i ].price;
     }
    
     // compute tax
     float Tax = total*TaxRate;
     float withTax = Total + tax;

     // report findings.

}


What you defined with char name[10] was a character array of 10 bites, and every time you read in fron cin, it would replace that one variable over and over. Thus the last one entered would be the last one you saw. If you wanted 10 names it would be char name[10] [10] a two dimensional array. That is why in my code example I did what I did with a struct and then made 10 instances of the struct with item itemArray[10] with all the relative information.
Last edited on
Thanks a Lot For Your Help!!!
Topic archived. No new replies allowed.